I want to add a JComboBox
in Swing that is simple but I want to assign the values for each items in combo. I have the following code
JComboBox
You can add an item as an object instead of adding String like this:
JComboBox jc = new JComboBox();
jc.addItem(item1);
jc.addItem(item2);
jc.addItem(item3);
So to return key, the function of the event is : jc.getSelectedItem().getKey
Doing this way you have to override the toString()
function of class ItemClass to return the string you want to show in combobox.
Btw, for return number, you may try : jc.getSelectedIndex()
, it'll return your index of your string (0 1 2
for "a" "b" "c"
)