简体   繁体   中英

Actionlistener/JComboBox clash

I'm trying to write a program which will take the input from one drop-down box, and use that to calculate the drop down box for another field, but I keep running into a problem. To make it work, I have to remove all items from on JComboBox, before refilling it, but this causes the program to throw an exception.

jbox1.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    autoCalc();

    String s = jbox1.getSelectedItem().toString();
     workS.set1(s);  
    jbox2.removeAllItems();
     for(int i = 0; i <= workS.jbox1.getSelectedItem; i++)
         {
         String temp = ("" + i);
         jbox2.addItem(temp);
         } 
        autoCalc();
     }
});


jbox2.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    autoCalc();      
    String s = jbox2.getSelectedItem().toString();
     workS.set2(s);  
        autoCalc();
     }
});

As far as I can tell, removing all items from jbox2 calls the jbox2 actionlistener, which realises that the field is empty and throws an exception. Does anyone have a way around this?

Keep a reference of the ActionListener added to jbox2. Before removing the items and filling with new list remove the ActionListener and readd after model is filled with new items.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM