SwingX : a One keyword and several suggestions

杀马特。学长 韩版系。学妹 提交于 2019-12-11 10:38:57

问题


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 ? http://www.jdocs.com/swingx/1.0/org/jdesktop/swingx/autocomplete/AutoCompleteDecorator.html

Thanks for helps.


回答1:


Given the code here:

  • http://www.jarvana.com/jarvana/view/org/swinglabs/swingx/1.6.1/swingx-1.6.1-sources.jar!/org/jdesktop/swingx/autocomplete/ListAdaptor.java?format=ok

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:

  • http://today.java.net/pub/a/today/2007/07/19/adding-auto-completion-to-swing-comboboxes.html


来源:https://stackoverflow.com/questions/9184350/swingx-a-one-keyword-and-several-suggestions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!