-2

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.)

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Volazh
  • 126
  • 2
  • 13
  • 1
    Your problem is that you're coding this as if it were a linear console program when it's not. Instead it's an event-driven GUI, and you need to learn which event you wish to respond to, and then how to respond to that event. Try reading the Swing tutorials to avoid guessing at this. – Hovercraft Full Of Eels May 27 '16 at 17:27
  • You can find links to the Swing tutorials and to other Swing resources here: [Swing Info](http://stackoverflow.com/tags/swing/info). – Hovercraft Full Of Eels May 27 '16 at 17:28

4 Answers4

2

You may try adding Button and then perform it's ActionListener and then enter the input and pressing the button will load the code of doing the stuff you want.

1

You can use a DocumentListener:

JTextField tf1 = new JTextField();
tf1.getDocument().addDocumentListener(DocumentListener()
    {
        @Override
        public void changedUpdate(DocumentEvent e)
        {
        }

        @Override
        public void insertUpdate(DocumentEvent e)
        {
            // parse here
        }

        @Override
        public void removeUpdate(DocumentEvent e)
        {
            // parse here
        }
    });
Jeff Miller
  • 1,424
  • 1
  • 10
  • 19
0

Instead of JTextField, you can use JOptionPane to get the user input. It will display a modal form and wait until the user validates.

String tfone = JOptionPane.showInputDialog("Please enter a number");
// Now test the user input
if (tfone != null) {
    int one = Integer.parseInt(tfone);
}

Here is the documentation User input with JOptionPane

tfosra
  • 581
  • 5
  • 12
  • 1
    I can't change Textfield by OptionPane cuz I'm working with GUI. – Volazh May 27 '16 at 17:12
  • Using JOptionPane will also generate a GUI form where you will write your value. Just try it first, I have edited my answer – tfosra May 27 '16 at 17:15
  • 1
    OptionPane generates a Graphics environment but no like JtextField does. i need exactly the square as Jtextfield does. with optionpane a can't generate as many squares as i want. – Volazh May 27 '16 at 17:17
  • 1
    How you even thought add 5, 20x20pixel squares into a Jframe with JoptionPane? – Volazh May 27 '16 at 17:25
  • The JOptionPane is not added into your JFrame but instead will be displayed as a modal dialog. After you hit the button Validate it will disapear and you retrieve your value – tfosra May 27 '16 at 17:27
  • 1
    Exactly that is what i don't want.. cuz i need squares listed as rows and columns, and i can make that by implementing Jtextfield or JtextArea instead of Joptionpane. – Volazh May 27 '16 at 17:29
  • Ok I see now. Just add a button with an ActionListener. In the `actionPerformed` method you can now retrieve the text from your JTextFields – tfosra May 27 '16 at 17:32
0

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.

Volazh
  • 126
  • 2
  • 13