I have an ArrayList object, which is a cd type. I want to add all the objects in a JList and show the name field to the user. I can add a String type to a JList, but what about a specific field of an object?
CD:
class CD{
public CD(String n){name = n;}
private String name;
public String getName(){return name;}
public void setName(String n){name = n;}
}
ArrayList:
ArrayList<CD> myList = new ArrayList<CD>();
And now I want to add myList to the JList:
JList list = new JList(myList);
panel.add(list);
JScrollPane scrol = new JScrollPane(list);
frame.add(scrol,BorderLayout.EAST);
frame.add(panel);
frame.setVisible(true);
First of all, is this correct? Secondly, how can I show the username field for an object in a list? my desired interface:

The left side is the name of my object! Thanks in advance. Bernard
source
share