问题
I am trying to test SwingX for the first time,For this, I read the doc : http://www.jdocs.com/swingx/1.0/org/jdesktop/swingx/autocomplete/AutoCompleteDecorator.html
I'd like to make a suggestion on a JTextField like this:
List items = [...];
JTextField textField = [...];
AutoCompleteDecorator.decorate(textField, items);
so I create a project on netbeans, this is my code:
package test_swingx;
import java.awt.Dimension;
import java.awt.HeadlessException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import org.jdesktop.swingx.autocomplete.AutoCompleteDecorator;
/**
*
* @author marwen
*/
public class Test_swingx extends JFrame {
public Test_swingx(String title) throws HeadlessException {
this.setTitle(title);
JPanel pan=new JPanel();
JTextField jtf=new JTextField();
jtf.setColumns(20);
List items = new ArrayList();
items.add("hello");
items.add("marwen");
items.add("allooo");
AutoCompleteDecorator.decorate(jtf, items);
pan.add(jtf);
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_swingx tsx=new Test_swingx("helloo swingx");
}
}
I get this error :
no suitable methode found for decorate....
I'm following well the syntax , I do not understand where the error come? ANY HELPS ?
回答1:
Your method decorate call, is resolve to the first method below which is incorrect. Second method decorate expected JList instead of list.
public static void decorate(JComboBox comboBox, ObjectToStringConverter stringConverter)
public static void decorate(JList list, JTextComponent textComponent)
However, if you still want to use List, you should use this method,
public static void decorate(JTextComponent textComponent, List<?> items, boolean strictMatching)
I've changed the error part in your question with this.
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.text.JTextComponent;
import org.jdesktop.swingx.autocomplete.AutoCompleteDecorator;
public class Test_swingx extends JFrame
{
public Test_swingx(String p_title)
{
this.setTitle(p_title);
JPanel pan = new JPanel();
JTextComponent jtf = new JTextField();
((JTextField) jtf).setColumns(20);
List items = new ArrayList();
items.add("hello");
items.add("marwen");
items.add("allooo");
AutoCompleteDecorator.decorate(jtf, items, false);
pan.add(jtf);
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_swingx tsx = new Test_swingx("helloo swingx");
tsx.setVisible(true);
}
}
来源:https://stackoverflow.com/questions/9169859/swingx-autocompletedecorator-no-suitable-methode-found-for-decorate