SwingX:一个关键字和几个建议

发布于 2025-01-03 14:18:06 字数 2755 浏览 2 评论 0原文

我想创建一个包含建议列表的 JTextField(例如 google、netbeans...),换句话说,当我在 JTextField 中写一个单词时,会显示一个列表...所以我尝试了这个:

import java.awt.BorderLayout;
import java.awt.HeadlessException;
import javax.swing.*;
import org.jdesktop.swingx.autocomplete.AutoCompleteDecorator;

/**
 *
 * @author marwen
 */
public class Test_swingx extends JFrame {

    public Test_swingx(String title) throws HeadlessException {
        JPanel pan = new JPanel();
        pan.setLayout(new BorderLayout());
        JTextField jtf = new JTextField(20);
        String[] tab = {"marwen", "marven", "mawww", "mamma", "ddd", "dddddd", "ppppp"};
        JList list = new JList(tab); //data has type Object[]
        AutoCompleteDecorator.decorate(list, jtf);
        pan.add(jtf, BorderLayout.NORTH);
        pan.add(list, BorderLayout.CENTER);

        setTitle(title);
        setContentPane(pan);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }

    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                Test_swingx tsx = new Test_swingx("helloo swingx");
            }
        });
    }
}

但我得到了这个错误:

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: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:98)
at  org.jdesktop.swingx.autocomplete.AutoCompleteDocument.setSelectedItem(AutoCompleteDocument.java:313)

通常它可以工作,我不明白文档中有错误? https://pirlwww.lpl.arizona.edu/resources/guide/software/SwingX/org/jdesktop/swingx/autocomplete/AutoCompleteDecorator.html

感谢您的帮助。

I'd like to create a JTextField with a list of suggestions(Like google,netbeans....),In other words when I wrote a word in my JTextField a list is displayed ... so I tried this :

import java.awt.BorderLayout;
import java.awt.HeadlessException;
import javax.swing.*;
import org.jdesktop.swingx.autocomplete.AutoCompleteDecorator;

/**
 *
 * @author marwen
 */
public class Test_swingx extends JFrame {

    public Test_swingx(String title) throws HeadlessException {
        JPanel pan = new JPanel();
        pan.setLayout(new BorderLayout());
        JTextField jtf = new JTextField(20);
        String[] tab = {"marwen", "marven", "mawww", "mamma", "ddd", "dddddd", "ppppp"};
        JList list = new JList(tab); //data has type Object[]
        AutoCompleteDecorator.decorate(list, jtf);
        pan.add(jtf, BorderLayout.NORTH);
        pan.add(list, BorderLayout.CENTER);

        setTitle(title);
        setContentPane(pan);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }

    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                Test_swingx tsx = new Test_swingx("helloo swingx");
            }
        });
    }
}

but i get this error :

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: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:98)
at  org.jdesktop.swingx.autocomplete.AutoCompleteDocument.setSelectedItem(AutoCompleteDocument.java:313)

normally it works, I do not understand there is an error in the doc ?
https://pirlwww.lpl.arizona.edu/resources/guide/software/SwingX/org/jdesktop/swingx/autocomplete/AutoCompleteDecorator.html

Thanks for helps.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等待圉鍢 2025-01-10 14:18:06

给出此处的代码:

引发异常的行是:

getTextComponent().setText(stringConverter.getPreferredStringForItem(list.getSelectedValue()));

在本例中,stringConverter 为 null。这解决了问题:

AutoCompleteDecorator.decorate(list, jtf, ObjectToStringConverter.DEFAULT_IMPLEMENTATION);

如果您还没有看到它,请看一下这篇文章:

Given the code here:

the line that throws the exception is:

getTextComponent().setText(stringConverter.getPreferredStringForItem(list.getSelectedValue()));

In this case, stringConverter is null. This solves the issue:

AutoCompleteDecorator.decorate(list, jtf, ObjectToStringConverter.DEFAULT_IMPLEMENTATION);

If you haven't seen it, take a look at this article:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文