Java Swing动态JComboBox
我已从数据库填充了组合框 B1。当 itemStateChanged 事件引发时,它应该填充另一个组合框 B2,但它不起作用。
ArrayList1 = //call method in database connection class()
for (int j = 0; j < ArrayList1.size(); j++)
{
if (j == 0)
{
combobox1.addItem("Select Any");
}
combobox1.addItem(ArrayList1.get(j));
}
combobox1.addItemListener(new ItemListener()
{
@Override
public void itemStateChanged(ItemEvent ie)
{
String catName = (String)combobox1.getSelectedItem();
if (!catName.equalsIgnoreCase("Select Any"))
{
ArrayList2=//call method in DB class with cat_name as argument
for(int i=0;i < ArrayList2.size();i++)
{
if (i == 0)
{
combobox2.addItem("Select Any");
}
combobox2.addItem(ArrayList2.get(i));
}
}
}
});
第一个组合框从数据库中填充,但在从中选择任何项目后,第二个组合框保持为空。
为什么我的电脑调试会挂起?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你必须实现 ComboBoxModel 并在
Model
中添加/删除/更改Items
,而不是在JComboBox
,也不是Array
、List
或Vector
中的某个位置,当然是可能的,但你必须执行你的代码在 EDT 上,并始终将Array
、List
或Vector
替换为具体的JComboBox
,不要这样做:-)也许你对Swing 中的并发,也许已完成更改,但在 EDT 之外,有关您的问题的更多信息将事件包装到invokeLater() 和 multiple-jcombobox
you have to implements ComboBoxModel and add/remove/change
Items
in theModel
, not in theJComboBox
, nor somewhere in theArray
,List
orVector
, sure is possible but you have to execute your code on EDT and always replaceArray
,List
orVector
for concretedJComboBox
, don't do it that this way :-)maybe you have problem with Concurency in the Swing, maybe changes are done, but outside EDT, more about your issues pass events wrapped into invokeLater() and multiple-jcombobox
问题解决了....
n ma problem get solved....
您必须阅读:
http://docs.oracle.com/javase/tutorial /uiswing/components/combobox.html
它将帮助您处理 java 组合框。
似乎您应该使用 ActionListener 作为事件来填充第二个组合框。
对于您的调试问题,您应该检查 java bugtracker 中的 bug 6714678
应该可以解决您的调试问题(自 2008 年起)
参见 2007 年相关的旧 jdks 无法工作 bug 6517045 说:
You must read:
http://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html
It will help you to deal with java comboboxes.
Seems you should use an ActionListener as event to populate second combobox.
For your debug problems you should check bug 6714678 from java bugtracker
should solve your debug problem (since 2008)
See could not work for old jdks as on 2007 related bug 6517045 says: