I have list of objects(Artikel.java) and i add them to JComboBox.
I want a property(String name) of this object be in the list? i did as below but stil the object is in the list and it is not readable.
how can i do it? Where is my problem?
if (titel.equalsIgnoreCase("artikel")) {
panel.lstArtikel = readFromArtikel();
panel.cbxArtikel = new WebComboBox(new ComboBoxModelArtikel(panel, lstArtikel));
panel.cbxArtikel.setAction(new ComboBoxArtikelActionListener(panel));
panel.cbxArtikel.setRenderer(new ArtikelListRenderer());
panel.artikelTabPanel.add(panel.cbxArtikel, BorderLayout.NORTH);
}
ComboBoxModelArtikel
public class ComboBoxModelArtikel implements ComboBoxModel{
ConfigToolScannersPanel panel; List<Artikel> lstArtikels;
private Object selectedItem;
public ComboBoxModelArtikel(ConfigToolScannersPanel panel, List<Artikel> artikels) {
this.panel=panel; this.lstArtikels=artikels;
}
public void setSelectedItem(Object anItem) {
selectedItem=anItem;
}
public Object getSelectedItem() {
return selectedItem;
}
public int getSize() {
return lstArtikels.size();
}
public Object getElementAt(int index) {
return (Artikel)lstArtikels.get(index);
}
public void addListDataListener(ListDataListener l) {
//Todo:
}
public void removeListDataListener(ListDataListener l) {
//Todo:
}
}
ArtikelListRenderer()
class ArtikelListRenderer extends JLabel implements ListCellRenderer {
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
if (value != null) {
Artikel artikels = (Artikel) value;
setText(artikels.getName());
} else{
setText("Please select an item");
}
return this;
}
}
I recreated the problem in one class, and it seems to be working:
Maybe the problem is in the WebComboBox