- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在测试 SwingX AutoCompleteDecorator,我使用的代码是这样的:
import javax.swing.text.JTextComponent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JPanel;
import javax.swing.JList;
import org.jdesktop.swingx.autocomplete.*;
public class Test extends JFrame
{
public Test()
{
this.setTitle("Testing");
JPanel pan = new JPanel();
JTextComponent test = new JTextField();
((JTextField)test).setColumns(20);
String[] data = {"one", "two", "three", "four"};
JList dataList = new JList(data);
AutoCompleteDecorator.decorate(dataList, test);
pan.add(test);
this.setContentPane(pan);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setBounds(280,150,500,200);
}
public static void main(String[] args)
{
Test t = new Test();
}
}
我最终收到大量运行时错误消息,我真的不想发布有关线程“AWT-EventQueue-0”java.lang.NullPointerException 中的异常的堆栈跟踪:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at org.jdesktop.swingx.autocomplete.ListAdaptor.valueChanged(ListAdaptor.java:77)
at javax.swing.JList.fireSelectionValueChanged(JList.java:1798)
at javax.swing.JList$ListSelectionHandler.valueChanged(JList.java:1812)
at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:184)
at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:164)
at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:211)
at javax.swing.DefaultListSelectionModel.changeSelection(DefaultListSelectionModel.java:405)
at javax.swing.DefaultListSelectionModel.changeSelection(DefaultListSelectionModel.java:415)
at javax.swing.DefaultListSelectionModel.setSelectionInterval(DefaultListSelectionModel.java:459)
at javax.swing.JList.setSelectedIndex(JList.java:2212)
at javax.swing.JList.setSelectedValue(JList.java:2362)
at org.jdesktop.swingx.autocomplete.ListAdaptor.setSelectedItem(ListAdaptor.java:99)
at org.jdesktop.swingx.autocomplete.AutoCompleteDocument.setSelectedItem(AutoCompleteDocument.java:355)
at org.jdesktop.swingx.autocomplete.AutoCompleteDocument.insertString(AutoCompleteDocument.java:303)
at javax.swing.text.JTextComponent.replaceSelection(JTextComponent.java:1386)
at javax.swing.text.DefaultEditorKit$DefaultKeyTypedAction.actionPerformed(DefaultEditorKit.java:884)
at javax.swing.SwingUtilities.notifyAction(SwingUtilities.java:1664)
at javax.swing.JComponent.processKeyBinding(JComponent.java:2879)
at javax.swing.JComponent.processKeyBindings(JComponent.java:2926)
at javax.swing.JComponent.processKeyEvent(JComponent.java:2842)
at java.awt.Component.processEvent(Component.java:6282)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1895)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:762)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:1027)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:899)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:727)
at java.awt.Component.dispatchEventImpl(Component.java:4731)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:729)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:688)
at java.awt.EventQueue$3.run(EventQueue.java:686)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:702)
at java.awt.EventQueue$4.run(EventQueue.java:700)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:699)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
我错过了什么?
最佳答案
这是一个错误:罪魁祸首是使用双参数方法用 JList 的项目装饰 textComponent 时的空 ObjectToStringConverter:
public static void decorate(JList list, JTextComponent textComponent) {
decorate(list, textComponent, null);
}
快速修复是使用三参数方法并传入默认转换器:
JTextComponent test = new JTextField(20);
String[] data = {"one", "two", "three", "four"};
JList dataList = new JList(data);
AutoCompleteDecorator.decorate(dataList, test, ObjectToStringConverter.DEFAULT_IMPLEMENTATION);
归档Issue #1570 - 从修订版 #4305 开始修复
晨间沉思(可以安全地忽略 :-)
技术原因是不正确的构造函数链接:插入默认值应该由全能构造函数处理(或者它应该抛出 NPE)
public ListAdaptor(JList list, JTextComponent textComponent) {
this(list, textComponent, ObjectToStringConverter.DEFAULT_IMPLEMENTATION);
}
public ListAdaptor(JList list, JTextComponent textComponent, ObjectToStringConverter stringConverter) {
this.list = list;
this.textComponent = textComponent;
this.stringConverter = stringConverter;
// when a new item is selected set and mark the text
list.addListSelectionListener(this);
}
根本原因是转换器所有权的微妙转变:它通常的所有者是处理自动完成的自定义文档,该文档保护自己免受 null。对于 JList 变体,它的 ListAdaptor 不习惯这种负担......这种转变并不是不正确的(事实上,确切的方法),只是引入了一个很容易被忽视的非常轻微的不一致。
关于java - SwingX AutoCompleteDecorator JTextField 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17956556/
我有一个 JComboBox,其中包含 200 个任务名称的列表。 JComboBox 中的初始选择为空白。 JComboBox combo = new JComboBox(); co
我正在使用 JTextField 进行自动建议,如下面的代码所述 String[] myStrings = new String[] {"Elem1","Elem2","Elem3","Elem4",
对于那些熟悉 SwingX 的 AutoCompleteDecorator 的人,我有一个关于处理 JComboBox 的弹出可见性的问题。我在当前的项目中使用了 AutoCompleteDecora
我正在测试 SwingX AutoCompleteDecorator,我使用的代码是这样的: import javax.swing.text.JTextComponent; import javax.
我在 JComboBox 上使用 SwingX AutoCompleteDecorator。一切工作正常,除了我希望允许我的用户更改我的对象的名称,该名称也显示在组合框中。问题是我可以刷新组合框,但自
我在 JPanel 中使用 JCombobox 和 editable = true,并在同一面板中使用 JTable,其中有一列设置为显示组合框作为其字段类型。我申请了 AutoCompleteDe
我是一名优秀的程序员,十分优秀!