Java Swing动态JComboBox

发布于 2024-12-04 13:47:05 字数 1043 浏览 1 评论 0 原文

我已从数据库填充了组合框 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));                   
            }                   
        }
    }           
});

第一个组合框从数据库中填充,但在从中选择任何项目后,第二个组合框保持为空。

为什么我的电脑调试会挂起?

I have populated a combobox B1 from database. When itemStateChanged event raises it should populate another combobox B2, but its not working.

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

first combobox gets populated from database, but after selecting any item from it second combobox keeps empty.

and why debugging this my computer hangs on?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

胡渣熟男 2024-12-11 13:47:05

你必须实现 ComboBoxModel 并在 Model 中添加/删除/更改 Items,而不是在JComboBox,也不是 ArrayListVector 中的某个位置,当然是可能的,但你必须执行你的代码在 EDT 上,并始终将 ArrayListVector 替换为具体的 JComboBox,不要这样做:-)

也许你对Swing 中的并发,也许已完成更改,但在 EDT 之外,有关您的问题的更多信息将事件包装到invokeLater()multiple-jcombobox

you have to implements ComboBoxModel and add/remove/change Items in the Model, not in the JComboBox, nor somewhere in the Array, List or Vector, sure is possible but you have to execute your code on EDT and always replace Array, List or Vector for concreted JComboBox, 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

枯叶蝶 2024-12-11 13:47:05
DefaultComboBoxModel model = new DefaultComboBoxModel(yourstringarray);
                    item_combobox.setModel( model );

问题解决了....

DefaultComboBoxModel model = new DefaultComboBoxModel(yourstringarray);
                    item_combobox.setModel( model );

n ma problem get solved....

向日葵 2024-12-11 13:47:05

您必须阅读:

http://docs.oracle.com/javase/tutorial /uiswing/components/combobox.html

它将帮助您处理 java 组合框。

似乎您应该使用 ActionListener 作为事件来填充第二个组合框。

对于您的调试问题,您应该检查 java bugtracker 中的 bug 6714678

-Dsun.awt.disablegrab=true

应该可以解决您的调试问题(自 2008 年起)

参见 2007 年相关的旧 jdks 无法工作 bug 6517045 说:

经过讨论,我们得出的结论是,这(组合框事件的调试)只是又一个地方,但事实并非如此
明智地停止在调试器中(对于 DnD、全屏也是如此)。

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

-Dsun.awt.disablegrab=true

should solve your debug problem (since 2008)

See could not work for old jdks as on 2007 related bug 6517045 says:

after discussion we came to conclusion that this (debug on combobox events) is just one more place when it is not
wise to stop in debugger (the same is true for DnD, fullscreen).

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