Sebastian
Sebastian

Reputation: 465

Using object created in Jbutton ActionPerformed method in Jlist

I am trying to design a user interface that allows the user to load a file containing information about persons into the system and then access information about single persons. I have a Method getpersons that gets information from an ArrayList and creates an object PersonList. The ArrayList is generated by reading information about people from a .txt file. PersonList again contains single Person objects. Now I created a JButton and in the EventListener method I call the first method. So basically upon clicking the button my program reads the file and creates an object PersonList. Then I would like to loop through the PersonList object and add person objects to the jlist. Upon clicking the object in the J list I would like to display the information contained in the person object in a JTextArea. Now my first thought was to pass the person objects directly to the Jlist. But I don't know how to get my JList to accept my specific person objects.

The person class looks somewhat like this:

public class Person 
{

    private String personname;
    private String personage; 
    private String personaddress; 
    .....
}

And my JButton code like this

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    ArrayList<String> personlist = readFile("data/persons.txt");
    PersonList persons = getpersons(personlist);


    for(int i = 0; i < persons.getsize(); i++)
    {

    JList.addItem(persons.getperson(i));
    }        
} 

Another idea would be to simply display the name of the person which is a string in the Jlist. Then I would compare the name the user clicks in the Jlist to my persons in the PersonList object and display the information of the corresponding person in the text area. But this would require me to get the PersonList object out of the jButton1ActionPerformed method which is private (and cannot be changed to public) and pass it to the JListActionPerformed method. Any help would be appreciated I am not sure whether this is a good approach. If you have suggestions for a better approach to doing this, I would also be glad.

Upvotes: 0

Views: 84

Answers (1)

camickr
camickr

Reputation: 324108

Then I would like to loop through the PersonList object and add person objects to the jlist.

You could just display the Person object in a JTable. Then you can display all the information about the Person in a different column. You would need to create a custom TableModel for this.

Check out Table Row Model for an example of how this might be done.

But I don't know how to get my JList to accept my specific person objects.

You can add any Object to the ListModel used by the JList, so you add the Person object to the JList the same way you add a String to the JList.

A JList uses a renderer to display the Object in the list. The default renderer just invokes the toString() method of the object to get the text to display. So a simple solution is to just implement a toString() method in your Person object to return the String you want to display in the list.

However it is not really a good idea to use the toString() method to contain data used by the application, so instead the better solution is to create a custom renderer to display the data.

Read the section from the Swing tutorial on Writing a Custom Renderer for more information and examples.

Upvotes: 2

Related Questions