2

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?

albfan
  • 12,542
  • 4
  • 61
  • 80
Geetanjali
  • 1,043
  • 3
  • 13
  • 37
  • 1
    Question: did you check that Array_list2 is not empty after call to DB? Adding a trace here would make sure the problem is not in the code you don't show in your example (the DB method). – jfpoilpret Sep 15 '11 at 08:27
  • but when i m going to debug it then it'll hang ma computer??then how can i debug it? – Geetanjali Sep 15 '11 at 08:30
  • 2
    please learn java naming conventions and stick to them – kleopatra Sep 15 '11 at 08:33
  • I agreed to ur point but i have made it randomly.I'll keep diz in mind.. – Geetanjali Sep 15 '11 at 08:36
  • 1
    @Geetanjali you don't need to perform debugging from your IDE, a simple trace with `System.out.println(...)` would be enough, we just want to ensure that `ArrayList2` is NOT empty. – jfpoilpret Sep 15 '11 at 09:26
  • @jfpoilpret: u r right my arraylist2 is empty so i have checked ma query n i got the combo box filled.thank u so so so so much.thanks a ton – Geetanjali Sep 16 '11 at 04:18

3 Answers3

2

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

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • 1
    +1 for suggestion of using a model. However I don't think this would solve the problem. – jfpoilpret Sep 15 '11 at 08:34
  • changes are done INSIDE the EDT because the code is executed from an `ItemListener`; hence there's no specific threading problem in that example (except the fact that DB access would better be performed with `SwingWorker` but that's not the source of the problem). – jfpoilpret Sep 15 '11 at 09:28
1
DefaultComboBoxModel model = new DefaultComboBoxModel(yourstringarray);
                    item_combobox.setModel( model );

n ma problem get solved....

Geetanjali
  • 1,043
  • 3
  • 13
  • 37
0

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

albfan
  • 12,542
  • 4
  • 61
  • 80