jcombobox populate with hashmap

ε祈祈猫儿з 提交于 2020-01-30 12:15:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!