Implementing Java2SAutoTextField to a JtextField

时光怂恿深爱的人放手 提交于 2019-12-12 09:01:40

问题


1) I was able to add a JTextField to the JFrame, and I initialized the class Java2sAutoTextField as given in Auto Complete JTextField (Swing / AWT / SWT / JFace forum at JavaRanch).

2) I initialized the list and field inside the JFrame constructor, as shown below.

List possible = new ArrayList(); 
possible.add("Austria"); 
possible.add("Italy"); 
possible.add("Croatia"); 
possible.add("Hungary"); 
Java2sAutoTextField autoCompleter = new Java2sAutoTextField(possible); 

3) The problem that arises is this: Even though I have initialized the Java2sAutoTextField, how can I apply auto completing to the JTextField?


回答1:


Adding the main() method below to Java2sAutoTextField produced the expected result after typing "H". It's not crucial for this example, but Swing GUIs should be constructed on the EDT.

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

        @Override
        public void run() {
            List<String> list = new ArrayList<String>(Arrays.asList(
                "Austria", "Croatia", "Hungary", "Italy"));
            JFrame f = new JFrame("AutoTest");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(new Java2sAutoTextField(list));
            f.pack();
            f.setLocationRelativeTo(null);
            f.setVisible(true);
        }
    });
}


来源:https://stackoverflow.com/questions/3673807/implementing-java2sautotextfield-to-a-jtextfield

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