Search code examples
javaswingjcomboboxpackagingrenderer

Java Swing setEditable(boolean) not working in JComboBox with custom renderer


I have a JComboBox of type myclass. I use CustomListRender to display one of the attributes of myclass, and it works fine.

Then I set myCombobox.setEditable(true). JComboBox becomes editable, but by default text is set to somthing like this in combobox:

 com.mypackagename.myclass

Can anyone tell me how to solve this problem?

Thanks in advance.


Solution

  • As discussed in How to Use Combo Boxes: Providing a Custom Renderer,

    The default renderer knows how to render strings and icons. If you put other objects in a combo box, the default renderer calls the toString method to provide a string to display.

    Unless overridden, you're probably seeing the toString() implementation inherited from Object. At a minimum, you'll need to override MyClass#toString() or update your renderer supply a custom editor accordingly; there's a related example here. Most would prefer the latter; your sscce showing your custom renderer would make it easier to suggest alternatives.