开发者

Java Swing dynamic JComboBox

开发者 https://www.devze.com 2023-04-05 05:00 出处:网络
I have populated a combobox B1 from database. When itemStateChanged event raises it should populate another combobox B2, but its not working.

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 wh开发者_高级运维y debugging this my computer hangs on?


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


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

n ma problem get solved....


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号