问题
I want to fill my combobox with a display value and a number value
HashMap<Integer, String> databaseList
I would to display the string in the combobox but the value of the comboxbox must be integer. I know this is posible in c# is this also possible in Java?
回答1:
I want to fill my combobox with a display value and a number value
I would create a separate Object to contain the two properties and then add these indivdual objects to the combo box.
Check out Combo Box With Hidden Data for an example of this approach.
jcombobox populate with hashmap
Otherwise you will need to create a custom ComboBoxModel
.
This would be more difficult to do since a Hashmap doesn't have a sequential ordering of the data that you can access directly, unless your Integer is a sequential number starting at 0.
You would then also need to implement a custom method to get the String value of any combo box item.
回答2:
Values of JComboBox can be anything, since it's generic template.
JComboBox<String> combobox = new JComboBox<>();
String str = "string part";
int i = 7;
combobox.addItem(str + i);
Or you can make your pair class and use that as template argument for combobox.
来源:https://stackoverflow.com/questions/30063197/jcombobox-populate-with-hashmap