Java Swing动态JComboBox [英] Java Swing dynamic JComboBox

查看:75
本文介绍了Java Swing动态JComboBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从数据库中填充了一个组合框 B1 .当itemStateChanged事件引发时,它应该填充另一个组合框 B2 ,但它不起作用.

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?

推荐答案

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

n个问题得到解决....

n ma problem get solved....

这篇关于Java Swing动态JComboBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆