This might be a dup – I can’t find it exactly though – I’m basically simply trying to customize a JComboBox display by providing my own ListCellRenderer:
targetCombo = new JComboBox();
targetCombo .setRenderer(new BasicComboBoxRenderer(){
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus){
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if (value != null){
MyObj myObj = (myObj)value;
setText(myObj.getName());
}
return this;
}
});
The component properly displays the name when I expand the JComboBox list. However, on item selection, the display reverts to the toString() value of myObj.
Am I missing something?
Replace the use of
BasicComboBoxRendererwithDefaultListCellRendererYou should never have a need to use the components from the look and feel packages, unless you are planning on creating your own look and feel.