如何在运行时更改 JComboBox 中的项目数组

发布于 2024-11-14 19:49:04 字数 117 浏览 6 评论 0原文

我将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

街角卖回忆 2024-11-21 19:49:04

您应该创建一个 而不是在构造函数中传递值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 be DefaultComboBoxModel, 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 the ComboBox.

沫尐诺 2024-11-21 19:49:04

如果您想在运行时在 JComboBox 中添加新项目,请使用 JComboBox 的 addItem() 方法。

If you want to add new items in JComboBox at runtime then use addItem() method of JComboBox.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文