How to use Map element as text of a JComboBox
Posted
by llm
on Stack Overflow
See other posts from Stack Overflow
or by llm
Published on 2010-05-11T17:12:15Z
Indexed on
2010/05/11
17:54 UTC
Read the original article
Hit count: 233
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.
© Stack Overflow or respective owner