问题
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