Search code examples
javamultithreadingswingjtextfieldevent-driven-design

TextField waiting for a user input


I got a JTextfield a GetText method, and an array to store the numbers logged on the Jtextfield.

JTextField tf1 = new JTextField();
frame.add(tf1);

String tfone = tf1.getText();
int one = Integer.parseInt(tfone);

int[][] array = new int[4][5]; 
array[0][0] = one;
array[0][1] = otherValues...

The problem here is, that code execute all, so no wait for a user input into the JtextField. How can i make the jtextfield wait, until an user log in something. To latter on execute the Integer.Parseint ?

I can no change JtextField by another method cuz I'm working with GUI (Graphic User Environment.)


Solution

  • Here is the code:

    JTextField tf1 = new JTextField();
    frame.add(tf1);
    
    JButton b = new JButton();
    b.setText("Solve");
    b.setBounds(30, 140, 110, 30);
    
    b.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) 
                {
                    String tfone = tf1.getText();
                    int one = Integer.parseInt(tfone);
    
                    int[][] array = new int[4][5]; 
                    array[0][0] = one;
                    array[0][1] = otherValues...
                 //Here you can complete the rest of functions
    

    });

    frame.add(b);
    

    Once the user press the button the code will end its execution.