3

我总是收到自动完成 JComboBox 的错误。为了自动完成我的 jcombobox,我使用了http://w_w.orbital-computer.de/JComboBox/这个脚本。当我创建它们时,我会这样做:

final Object[] elements = new Object[] {"Please refresh your list"};
comboBox[i] = new JComboBox(elements);
comboBox[i].setEditable(true);                                  
new AutoCompleteJComboBox(comboBox[i]);

如果我只是将它与新的 jcombobox 一起使用,它就可以正常工作,但是如果我像这样更改它们的值:

comboBox[i].removeAllItems();    
comboBox[i].setModel(new JComboBox<>(ListWithNames).getModel());
comboBox[i].setEditable(true);
new AutoCompleteJComboBox(comboBox[i]);

我收到以下错误消息:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: bad position: 11
    at javax.swing.text.JTextComponent.setCaretPosition(Unknown Source)
    at src.AutoCompleteJComboBox.highlightCompletedText(AutoCompleteJComboBox.java:146)
    at src.AutoCompleteJComboBox.access(AutoCompleteJComboBox.java:145)
    at src.AutoCompleteJComboBoxactionPerformed(AutoCompleteJComboBox.java:30)
    at javax.swing.JComboBox.fireActionEvent(Unknown Source)
    at javax.swing.JComboBox.setSelectedItem(Unknown Source)
    at javax.swing.JComboBox.setSelectedIndex(Unknown Source)
    at javax.swing.plaf.basic.BasicComboPopup$Handler.mouseReleased(Unknown Source)
    at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at javax.swing.plaf.basic.BasicComboPopupprocessMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access(Unknown Source)
    at java.awt.EventQueuerun(Unknown Source)
    at java.awt.EventQueuerun(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomaindoIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomaindoIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueuerun(Unknown Source)
    at java.awt.EventQueuerun(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomaindoIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

这个位置 11 是 ListWithNames 中第一个值的长度,此外,列表中的第一项仍然可以正常工作,但其他项只是突出显示的文本的第一个字母,然后出现错误。

AutoCompleteJComboBox.java 是这个文件在第 146 行的另一个名称是

editor.setCaretPosition(this.getLength());

我希望有人可以帮助我。

(很抱歉顶部的链接,但我不允许发布超过两个)

4

0 回答 0