I want to creat a GUI with buttons so that each of them will activate a textfield. and in the textfield I want to type info and press enter so that the program will receive it and the textfield will keep staying so that I would be able to type more and press enters to input more info to the program. And as soon as I'm done I would type something like "done" and then the program will continue to execute the next lines
this is an example for one button so far.
This is my code:
.public class runtime extends JFrame { setLayout(new FlowLayout()); button1 = new JButton("press me"); add(button1); ActionHandle userClick = new ActionHandle();//create object from the action handling class button1.addActionListener(userClick); }
.private class ActionHandle implements ActionListener { if(click.getSource() == button1) { method1(); } else //if haven't pressed any button then must've entered something in the text field { String input = new String(); input = textField1.getText(); System.out.println(input); } }
The whole code here is in one class, and I activate it using a different class with main.private void method1() { JTextField text1 = new JTextField(20); //{ add(text1); ActionHandle userClick = new ActionHandle(); text1.addActionListener(userClick); // create the textfield line setLayout(new FlowLayout()); this.setVisible(true); // } String userInput= ""; //here I will put all the info received from user while(!userInput.equals("done")) //as long as user didn't type "done" { userInput = userInput + _________ // I don't know what to write here } }
My problem is that the textfield doesn't wait for me to write anything it just continue to execute the rest of the code (it goes to the while loop).
Please tell me what I need to do in order to receive such info, then use it, and continue receiving info until I type "done". Just remember that I need to do the same for at least 13 other buttons. I don't want to use normal JOptionPane I need the JTextField.
If you edit my code just delete all my commets from it and add your own on your edit. Thank you guys