使用新值填充JComboBox

Ann*_*lee 1 java swing jcombobox

目前我正在填充我的JComboBox:

countryBox = new JComboBox(countryList.toArray(new String[countryList.size()]));
Run Code Online (Sandbox Code Playgroud)

但是,在使用我的程序时countryList,我希望以不同的方式填充我的JComboBox.我尝试使用该操作来更改我的JComboBox:

countryBox.addActionListener(new ActionListener() {
   countryBox = new JComboBox(countryList.toArray(new String[countryList.size()]));
}
Run Code Online (Sandbox Code Playgroud)

但是,它并没有改变它的价值.对我来说,countryBox似乎预先填充了之前的数据.任何建议我能做什么?

我感谢你的回答!

Mad*_*mer 7

不要创建新的JComboBox,创建新模型

DefaultComboBoxModel model = new DefaultComboBoxModel(countryList.toArray(new String[countryList.size()]));
countryBox.setModel(model);
Run Code Online (Sandbox Code Playgroud)

您可以创建自己的ComboBoxModel代理当前的List,但这取决于您.

仔细查看如何使用组合框了解更多详细信息