Fill a JComboBox with new values

早过忘川 提交于 2019-12-11 09:59:18

问题


currently I am filling my JComboBox like that:

countryBox = new JComboBox(countryList.toArray(new String[countryList.size()]));

However, when using my program the countryList changes and I would like to fill my JComboBox differently. I tried to use the action to change my JComboBox:

countryBox.addActionListener(new ActionListener() {
   countryBox = new JComboBox(countryList.toArray(new String[countryList.size()]));
}

However, it does not change its values. For me it seems that the countryBox is prefilled with the data from before. Any recommendations what I could do?

I appreciate your answer!


回答1:


Don't create a new JComboBox, create a new model

DefaultComboBoxModel model = new DefaultComboBoxModel(countryList.toArray(new String[countryList.size()]));
countryBox.setModel(model);

You could create your own ComboBoxModel which could proxy you current List, but that's up to you.

Take a closer look at How to Use Combo Boxes for more details



来源:https://stackoverflow.com/questions/25358545/fill-a-jcombobox-with-new-values

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