更改自动完成JComboBox的值

时间:2011-12-30 01:08:48

标签: java autocomplete jcombobox illegalargumentexception

我总是收到自动完成JComboBox的错误。 要自动完成我的jcombobox,请使用thishttp://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]);

如果我只使用新的jcomboboxes它会正常工作,但如果我改变它们的值如下:

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是this file,第146行中有另一个名字

editor.setCaretPosition(this.getLength());

我希望有人能帮助我。

(对不起顶部的链接,但我不允许发布超过两个)

0 个答案:

没有答案