0

Okay, I've been searching how to do an auto prediction textfield for days now, and yes I found some solutions but they are completely hard to understand to be honest, and totally confusing since I'm new to Java/GUI. It would have been much easier if I had to click a button to do it, but I cant get how the program will perform such action whenever "a letter gets written". I've made a simple textfield and a button, whenever the button is clicked, the string in the textfield gets added in an arraylist, then prints the whole arraylist in another textfield(Just a simple example to test the auto prediction)

public class Phonebook {
    public static ArrayList<String> names = new ArrayList<String>();

    public static void main(String[] args) {
        JFrame myForm = new JFrame("Phonebook");
        myForm.setSize(555, 500);
        myForm.setLocation(0, 0);

        JButton button = new JButton("Add");
        button.setSize(100, 50);
        button.setLocation(450, 40);
        myForm.add(button);

        JTextField t = new JTextField();
        t.setSize(200, 60);
        t.setLocation(10, 40);
        myForm.add(t);

        JTextField ttt = new JTextField();
        ttt.setSize(500, 300);
        ttt.setLocation(10, 100);
        ttt.setEditable(false);
        myForm.add(ttt);

        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                names.add(t.getText());
                String str = "";
                for(int i=0; i<names.size(); i++)
                    str + =names.get(i) + "\n";
                ttt.setText(str);
            }
        });
        myForm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myForm.setLayout(null);
        myForm.setVisible(true);
    }
}

So I want the big textfield to auto complete the small textfield, so if I type "M", it shows only the names in the arraylist that start with an "M", the code for finding the names that start with an "M" would be easy, but making it "Automatic" sounds very difficult to me. If anyone could help me with my code instead of sending me a new whole confusing code, I would really appreciate that. Thank you.

Edit: Or I just want the code that somehow checks if a letter is written, so (if a letter gets written in the textfield), system.out.print("A");

Markus Mitterauer
  • 1,560
  • 1
  • 14
  • 28
Ahmed
  • 49
  • 1
  • 7
  • http://stackoverflow.com/questions/14186955/create-a-autocompleting-textbox-in-java-with-a-dropdown-list Is that what you want? – Mehraj Malik Sep 19 '16 at 10:43
  • btw these are called **Auto complete text-box**. Search on google you will 1000 of solutions. – Mehraj Malik Sep 19 '16 at 10:45
  • @MehrajMalik I've been reading through this for hours now, and every link I click, gives me another 10 links, I got through thousand of links. They are all doing the auto prediction or the auto complete inside a combobox, but I want it inside a textfield, which is different I suppose – Ahmed Sep 19 '16 at 13:39
  • @MehrajMalik I've added a small edit at the bottom, if you can have a look – Ahmed Sep 19 '16 at 14:40

1 Answers1

1

You could try attaching a Document Listener to the text box:

textField.getDocument().addDocumentListener(new DocumentListener() {
    public void insertUpdate(DocumentEvent e) {
        // search the prediction data for the current contents 
        // of the text field
    }
    public void removeUpdate(DocumentEvent e) {
        // do stuff
    }
    public void changedUpdate(DocumentEvent e) {
        //Plain text components do not fire these events
    }
});

You can then use either the insertUpdate or removeUpdate functions to get a hook into the point when text is changed, access the textFields values and put your auto-complete functionality in there.

dahui
  • 2,128
  • 2
  • 21
  • 40
  • It worked. However, I cann't understand what changedUpdate does when the 2 other functions are doing what needs to be done.Thanks alot for the answer tho. – Ahmed Sep 19 '16 at 14:54
  • You're welcome. The documentation I linked explains, changedUpdate is for a StyledDocument object, so it detects changes in style, not changes in the text content – dahui Sep 19 '16 at 15:15