用新值填充JComboBox

用新值填充JComboBox

本文介绍了用新值填充JComboBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在像这样填充我的JComboBox:

currently I am filling my JComboBox like that:

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

但是,当使用我的程序时,countryList会发生变化,我想以不同的方式填充我的JComboBox.我尝试使用该操作来更改我的JComboBox:

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()]));
}

但是,它不会更改其值.对我来说,countryBox似乎已经预先填充了以前的数据.有什么建议我可以做什么?

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?

感谢您的回答!

推荐答案

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

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

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

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

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

仔细研究如何使用组合框了解更多详情

这篇关于用新值填充JComboBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 04:42