I have an ArrayList of object which is type of the cd. I want to add all the objects to the JList and show the field of name to the user. I can add the String type to the JList but what about specific field of the object?
CD is:
class CD{
public CD(String n){name = n;}
private String name;
public String getName(){return name;}
public void setName(String n){name = n;}
}
ArrayList is:
ArrayList<CD> myList = new ArrayList<CD>();
And now I want to add myList to a 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 way correct? Secondly how can I show the user name field of the object in the list? my desired interface is :
The left side is the name of my object! Thanks in advance. Bernard
Use a custom renderer based on the DefaultListCellRenderer:
import java.awt.Component;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
public class TestJList {
class CD {
public CD(String n) {
name = n;
}
private String name;
public String getName() {
return name;
}
public void setName(String n) {
name = n;
}
}
protected void initUI() {
List<CD> cds = new ArrayList<CD>();
cds.add(new CD("MJ - Bad"));
cds.add(new CD("Mozart - Concerto 123"));
cds.add(new CD("Nadeah - Odile"));
JFrame frame = new JFrame(TestJList.class.getSimpleName());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JList list = new JList(new Vector<CD>(cds));
list.setVisibleRowCount(10);
list.setCellRenderer(new DefaultListCellRenderer() {
@Override
public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
Component renderer = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if (renderer instanceof JLabel && value instanceof CD) {
// Here value will be of the Type 'CD'
((JLabel) renderer).setText(((CD) value).getName());
}
return renderer;
}
});
frame.add(new JScrollPane(list));
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new TestJList().initUI();
}
});
}
}
I think that if you want to work with the CD
class directly with JList
, you'll need to write your own renderer, that will work with CD
class. The renderer may extend the JLabel
and display the contents of the name
field.
There's also other way: for()
that iterates over the ArrayList
and adds only the name
field to the DefaultListRenderer
. The way that you'll do it is your choice.
You can also add ListSelectionListener
to show the album details of the selected list item.
All that you need to know is here:
// Thanks for kleopatra :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With