简体   繁体   English

是否可以使用jtextfield和Jlist进行自动完成?

[英]Is it possible to have an autocomplete using jtextfield and a Jlist?


I want to create an auto-complete program in java which should provide a list of suggestions instantly when the user types a character/String inside a JTextfield . 我想在java中创建一个自动完成程序,当用户在JTextfield键入字符/字符串时,它应立即提供建议列表。 The problem is that I am confused on how to do it. 问题是我对如何做到这一点很困惑。

Could somebody provide an idea or a sample on the said problem? 有人可以就上述问题提供想法或样本吗?

1) you have to sort your array before use for better performance... 1)您必须在使用前对阵列进行排序以获得更好的性能......

2) as I mentioned you have to take these two clasess 2)正如我所提到的,你必须采取这两个方面

3) don't forget set initial value for better and nicest work with these Components 3)不要忘记设置初始值,以便更好,更好地使用这些组件

simple output 简单的输出

在此输入图像描述

from code 来自代码

import java.awt.*;
import java.util.ArrayList;
import javax.swing.*;

public class AutoCompleteTextField {

    private JFrame frame;
    private ArrayList<String> listSomeString = new ArrayList<String>();
    private Java2sAutoTextField someTextField = new Java2sAutoTextField(listSomeString);
    private ArrayList<String> listSomeAnotherString = new ArrayList<String>();
    private Java2sAutoComboBox someComboBox = new Java2sAutoComboBox(listSomeAnotherString);

    public AutoCompleteTextField() {
        listSomeString.add("-");
        listSomeString.add("Snowboarding");
        listSomeString.add("Rowing");
        listSomeString.add("Knitting");
        listSomeString.add("Speed reading");
        listSomeString.add("Pool");
        listSomeString.add("None of the above");
//
        listSomeAnotherString.add("-");
        listSomeAnotherString.add("XxxZxx Snowboarding");
        listSomeAnotherString.add("AaaBbb Rowing");
        listSomeAnotherString.add("CccDdd Knitting");
        listSomeAnotherString.add("Eee Fff Speed reading");
        listSomeAnotherString.add("Eee Fff Pool");
        listSomeAnotherString.add("Eee Fff None of the above");
//
        someTextField.setFont(new Font("Serif", Font.BOLD, 16));
        someTextField.setForeground(Color.black);
        someTextField.setBackground(Color.orange);
        someTextField.setName("someTextField");
        someTextField.setDataList(listSomeString);
//
        someComboBox.setPrototypeDisplayValue("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
        someComboBox.setFont(new Font("Serif", Font.BOLD, 16));
        someComboBox.setForeground(Color.black);
        someComboBox.setBackground(Color.YELLOW);
        someComboBox.getEditor().selectAll();
        someComboBox.getEditor().getEditorComponent().setBackground(Color.YELLOW);
        ((JTextField) someComboBox.getEditor().getEditorComponent()).setDisabledTextColor(Color.black);
        someComboBox.setName("someComboBox");
        someComboBox.setDataList(listSomeAnotherString);
//
        frame = new JFrame();
        frame.setLayout(new GridLayout(0, 1, 10, 10));
        frame.add(someTextField);
        frame.add(someComboBox);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocation(100, 100);
        frame.pack();
        frame.setVisible(true);
//
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                someTextField.setText("-");
                someComboBox.getEditor().setItem(0);
                someComboBox.getEditor().selectAll();
                someTextField.grabFocus();
                someTextField.requestFocus();
                someTextField.setText(someTextField.getText());
                someTextField.selectAll();
            }
        });

    }

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

            @Override
            public void run() {
                AutoCompleteTextField aCTF = new AutoCompleteTextField();
            }
        });
    }
}

SwingX has an autocomplete feature, it's a decorator which can be applied to several component types. SwingX具有自动完成功能,它是一个可以应用于多种组件类型的装饰器。 It differs from what you are implementing in that it doesn't narrow the list of items. 它与您实施的不同之处在于它不会缩小项目列表。 Code is free, you probably can adjust to your needs 代码是免费的,您可以根据自己的需要进行调整

The latest release is version 1.6.4. 最新版本是1.6.4版。 Its resources (binaries, source, javadoc) is available in the project download area or via maven. 其资源(二进制文件,源代码,javadoc)可在项目下载区域或maven中使用。 For a first look of the functionality you might want to run the webstartable, available on the homepage. 首次查看功能,您可能希望运行主页上提供的webstartable。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM