Removing all Items from a combo box in Java

前端 未结 9 1134
自闭症患者
自闭症患者 2021-02-06 11:04

I need to remove all items from the combo box

    int itemCount = combo.getItemCount();

    for(int i=0;i

        
相关标签:
9条回答
  • 2021-02-06 11:34

    You can use

    this.combo.removeAllItems();

    to remove all the items in JComboBox.

    0 讨论(0)
  • 2021-02-06 11:35

    In second line:

    combo.removeItemAt(0);

    I think instead of 0 it should be i.

    do it in reverse order as:

    for(int i=combo.getItemCount()-1;i>=0;i--){
        combo.removeItemAt(i);
    }
    

    But in my case combo.removeAllItems() works fine

    0 讨论(0)
  • 2021-02-06 11:36

    The code in the question would normally work. However, it looks like a threading issue. Another thread may be messing with the items.

    However, I sugeest you should better use the removeAllItems(); method:

    combo.removeAllItems();
    
    0 讨论(0)
提交回复
热议问题