removeAllItems()

时间:2017-01-11 15:58:28

标签: java swing jcombobox indexoutofboundsexception updating

我试图以ports.removeAllItems();ports来致电JComboBox,我收到此错误

Exception in thread "Thread-3" java.lang.IndexOutOfBoundsException: bitIndex < 0: -1
at java.util.BitSet.get(BitSet.java:623)
at javax.swing.DefaultListSelectionModel.clear(DefaultListSelectionModel.java:278)
at javax.swing.DefaultListSelectionModel.setState(DefaultListSelectionModel.java:584)
at javax.swing.DefaultListSelectionModel.removeIndexInterval(DefaultListSelectionModel.java:652)
at javax.swing.plaf.basic.BasicListUI$Handler.intervalRemoved(BasicListUI.java:2601)
at javax.swing.AbstractListModel.fireIntervalRemoved(AbstractListModel.java:179)
at javax.swing.DefaultComboBoxModel.removeAllElements(DefaultComboBoxModel.java:174)
at javax.swing.JComboBox.removeAllItems(JComboBox.java:771)
at view.MainFrame.lambda$startPortsUpdate$3(MainFrame.java:188)
at java.lang.Thread.run(Thread.java:745)

此错误发生在我定期更新JComboBox

内容的函数中
private void startPortsUpdate() {
    Thread t = new Thread(() -> {
        while (currentCard.equals(IView.PORT)) { // while we're in port-selection screen
            try {
                Thread.sleep(500);
                ports.removeAllItems();
                for (Object o : getSerialPorts()) {
                    ports.addItem(o);
                }
                ports.revalidate();
            } catch (InterruptedException ex) {
                Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    });
    t.start();
}

我觉得很难复制,也不知道该怎么办。目前这不是一个大问题,但如果有人知道造成这种情况的原因,我将不胜感激。另外,如果您有任何建议以更好的方式更新我的JComboBox,请随时提出建议。

0 个答案:

没有答案