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");