编程语言
首页 > 编程语言> > java-JTextField自动完成错误

java-JTextField自动完成错误

作者:互联网

this thread中,我找到了一种在JTextField(和JComboBox,但没有问题)中实现AutoComplete功能的方法.

尝试在JTextField中实现此功能时,它将引发一些异常.以下代码将演示该问题.它是如此简单,与我在JComboBox中使用的方式非常相似,效果很好.

有人可以帮我找到解决方案吗?

这是代码:
(请注意,我正在使用swingx)

import javax.swing.JList;
import org.jdesktop.swingx.JXFrame;
import org.jdesktop.swingx.JXTextField;
import org.jdesktop.swingx.autocomplete.AutoCompleteDecorator;

public class ErrorDemo {

    public static void main(String[] args) {
        JXFrame frame = new JXFrame("Error Demo");
        JXTextField field = new JXTextField();
        String[] users = {"aaa", "bbb", "ccc"};
        AutoCompleteDecorator.decorate(new JList(users), field);

        frame.getContentPane().add(field);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setDefaultCloseOperation(JXFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

这是我得到的错误信息:
(有点长:D)

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at org.jdesktop.swingx.autocomplete.ListAdaptor.valueChanged(ListAdaptor.java:76)
    at javax.swing.JList.fireSelectionValueChanged(JList.java:1765)
    at javax.swing.JList$ListSelectionHandler.valueChanged(JList.java:1779)
    at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:167)
    at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:147)
    at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:194)
    at javax.swing.DefaultListSelectionModel.changeSelection(DefaultListSelectionModel.java:388)
    at javax.swing.DefaultListSelectionModel.changeSelection(DefaultListSelectionModel.java:398)
    at javax.swing.DefaultListSelectionModel.setSelectionInterval(DefaultListSelectionModel.java:442)
    at javax.swing.JList.setSelectedIndex(JList.java:2179)
    at javax.swing.JList.setSelectedValue(JList.java:2295)
    at org.jdesktop.swingx.autocomplete.ListAdaptor.setSelectedItem(ListAdaptor.java:98)
    at org.jdesktop.swingx.autocomplete.AutoCompleteDocument.setSelectedItem(AutoCompleteDocument.java:317)
    at org.jdesktop.swingx.autocomplete.AutoCompleteDocument.insertString(AutoCompleteDocument.java:265)
    at javax.swing.text.JTextComponent.replaceSelection(JTextComponent.java:1358)
    at javax.swing.text.DefaultEditorKit$DefaultKeyTypedAction.actionPerformed(DefaultEditorKit.java:859)
    at javax.swing.SwingUtilities.notifyAction(SwingUtilities.java:1636)
    at javax.swing.JComponent.processKeyBinding(JComponent.java:2849)
    at javax.swing.JComponent.processKeyBindings(JComponent.java:2884)
    at javax.swing.JComponent.processKeyEvent(JComponent.java:2812)
    at java.awt.Component.processEvent(Component.java:5815)
    at java.awt.Container.processEvent(Container.java:2058)
    at java.awt.Component.dispatchEventImpl(Component.java:4410)
    at java.awt.Container.dispatchEventImpl(Container.java:2116)
    at java.awt.Component.dispatchEvent(Component.java:4240)
    at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1848)
    at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:697)
    at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:962)
    at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:834)
    at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:661)
    at java.awt.Component.dispatchEventImpl(Component.java:4282)
    at java.awt.Container.dispatchEventImpl(Container.java:2116)
    at java.awt.Window.dispatchEventImpl(Window.java:2429)
    at java.awt.Component.dispatchEvent(Component.java:4240)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)

解决方法:

您的代码中没有JList.我认为应该读这样的东西…

        org.jdesktop.swingx.autocomplete.AutoCompleteDecorator.decorate(field, Arrays.asList(users), false);

如果要将JList绑定到TextField(以便文本字段将更新JList的选择),则需要装饰(JList列表,JTextComponent textComponent,ObjectToStringConverter stringConverter)…

标签:swingx,java,nullpointerexception,autocomplete,jtextfield
来源: https://codeday.me/bug/20191013/1906515.html