如何在运行时更改 JComboBox 中的项目数组
我将 JComboBox
添加到 JPanel
(在 JComboBox
构造函数中,我加载字符串数组)。如何在运行时更改该数组以加载新项目?
I add a JComboBox
to a JPanel
(in the JComboBox
constructor I load array of strings). How can I change that array at runtime, to load new items?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该创建一个 而不是在构造函数中传递值
ComboBoxModel
派生类并将其传递给构造函数。一个简单的例子是DefaultComboBoxModel
,但如果这不能满足您的需求,您可以创建自己的。然后您可以修改模型并将更改应用于组合框
。Rather than pass your values in the constructor, you should create a
ComboBoxModel
derived class and pass that to the constructor. A simple example would beDefaultComboBoxModel
, but you could create your own if that didn't satisfy your needs. Then you can modify the model and the changes are applied to theComboBox
.如果您想在运行时在 JComboBox 中添加新项目,请使用 JComboBox 的
addItem()
方法。If you want to add new items in JComboBox at runtime then use
addItem()
method of JComboBox.