Carol.Kar
Carol.Kar

Reputation: 5355

Fill a JComboBox with new values

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!

Upvotes: 1

Views: 96

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347334

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

Upvotes: 7

Related Questions