Is it possible to have an autocomplete using jtextfield and a Jlist?
Asked Answered
C

2

14


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. The problem is that I am confused on how to do it.

Could somebody provide an idea or a sample on the said problem?

Chura answered 31/8, 2011 at 10:26 Comment(1)
similar question: #6674962Nisa
M
18

1) you have to sort your array before use for better performance...

2) as I mentioned you have to take these two clasess

3) don't forget set initial value for better and nicest work with these Components

simple output

enter image description here

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();
            }
        });
    }
}
Malachy answered 31/8, 2011 at 10:54 Comment(4)
But can I have a dropdown autocomplete when I type in the textfield, just like in the Google?Chura
sure that would be little bit complicated, just redirect selected or correlated to the JWindow or to the un-decorated JDialog download.oracle.com/javase/tutorial/uiswing/components/…, which contains JList download.oracle.com/javase/tutorial/uiswing/components/…Malachy
yeah, you right. its complicated, I dont know if I can do it. Do you have some sample for that one in swingX?Chura
since I big SwingX fan... but this steamboat sailing another direction of mine :-), their ideas are excelent, but without any progess long time, maybe I wrong with de-gres and should be better to ask this question directly @kleopatraMalachy
M
13

SwingX has an autocomplete feature, it's a decorator which can be applied to several component types. 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. Its resources (binaries, source, javadoc) is available in the project download area or via maven. For a first look of the functionality you might want to run the webstartable, available on the homepage.

Moye answered 31/8, 2011 at 10:36 Comment(6)
could you help me how to download it cause I'm a newbie about this "SwingX"?Chura
do you have a sample for the swingX autocomplete? I have download "swingx-ws.jar", is this right?Chura
@Mikel - no (where did you get that? it's a different project), the name is most probably something like swingx.jar, swingx-src.jar or something like that (sorry, can't check as I cant access java.net right now, seems to be down to me) If you can access the site then run the webstartable for an example, it comes with a code view of the demo codeMoye
@Moye - still I cant access the site you give. What usually time does the site would up? I mean do you have any idea about the time that the site would be available?Chura
I have downloaded swingx-1.6.jar and swingx-1.6-sources.jar, is this right?Chura
@Moye i am using the AutoCompleteDecorator For ComboBox But The Focus Is Lost on each input character and transfer to the other Control (i.e. TextBox In My Case) jdocs.com/swingx/1.0/org/jdesktop/swingx/autocomplete/…Paraphrase

© 2022 - 2024 — McMap. All rights reserved.