I am populating a JComboBox (using addItem()) with all the elements of a collection. Each element in the collection is a HashMap (so its a ComboBox of Hashmaps..).
My question is – Given that I need each item to be a HashMap how do I set the text to apear in the combobox on the GUI? It needs to be the value of a certain key in the map. Normally if I am populating a combobox with my own type, I would just overide the toString() method…but I am not sure how to acheive this since I am using a Java HashMap.
Any ideas (if possible, without implementing my own HashMap)?
Update: It seems like there isn’t anyway to avoid having the object int the JComboBox overide toString() if I want custom functionality..I wish there was a way to (1) specify the objects to be loaded into the JComboBox and (2) specify how these objects are to appear in the GUI.
You can add any Object to the model and then create a custom renderer to display the object any way you want. Simple example that shows the toString() approach and custom renderer approach: