-2

I have been working on a program that gets the text from a textfield in a JFrame. here is the code for getting the text:

JTextField textField = new JTextField(a3);

String text = textField.getText();

    if (text.equals("hi")){
        textArea.setText("Hey!");
    }`

When I run the code everything works fine and the JFrame comes up. But when I change the text and then press enter, it does nothing. Do I need to use an ActionListener?

Steampunkery
  • 3,839
  • 2
  • 19
  • 28
  • 2
    You need to make an ActionListener for the Text box. That code you have checks the Text box once right after it's created, and never again. Andy Brown's link is a good one, check there. – DaaaahWhoosh Jan 29 '15 at 18:29
  • "And I'm probably going to be down voted for this, but think before you click." Statements like this don't need to be said. StackOverflow has rules and regulations for a reason. Don't you trust us? – SpacePrez Jan 29 '15 at 18:35

4 Answers4

1

Code executes in sequence. The above code means:

  • create a JTextField
  • immediately get its text (empty string)
  • immediately compare its text to "hi".

What you want is to re-execute this test when some GUI even occurs. For example, when the user clicks on a button, or when the user presses enter in the text field. You'll thus need to attach an event listener to the button (or to the text field) to tell Swing: when the button is clicked (or when enter is pressed in the field), I want this piece of code to be executed.

See the Swing tutorial about events for more information.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
1

You will need to set an actionListener. This will respond to the Enter keypress and invoke the listener. This whole process, and what listeners are, is documented in The Java Tutorials > How to use Text Fields

JPanel panel = new JPanel();
final JTextField textField = new JTextField(20);
final JTextArea textArea = new JTextArea(2,20);
panel.add(textField);
panel.add(textArea);
frame.getContentPane().add(panel);
textField.addActionListener(new ActionListener() {
    @Override public void actionPerformed(ActionEvent e) {
        String text = textField.getText();
        if (text.equals("hi")) {
            textArea.setText("Hey!");
        }
    }
});

Note that the text field and area need to be final (if they are local variables) in order to use them inside the listener if you create it like this as it is an anonymous class.

Andy Brown
  • 18,961
  • 3
  • 52
  • 62
  • is there a way for me to have multiple of these so that it can respond to multiple things? – Steampunkery Jan 29 '15 at 18:54
  • @Nitroman. You can register as many listeners as you like with an event source. You should read up on the [Swing event model](http://docs.oracle.com/javase/tutorial/uiswing/events/). – Andy Brown Jan 29 '15 at 18:56
  • Thanks, that article really helped and my AI is really coming along now. – Steampunkery Jan 30 '15 at 17:03
0

You have to set a listener for textfield and inside the listener check your code.

Prinz Km
  • 323
  • 1
  • 12
0

The code you are executing in the if conditional to check the text area is executed immediately after creating the text field. Then after you program has run for a few seconds you change the text field, and there is no code being executed which checks the text field again. This simply doesn't work.

Code is executed line by line, and one at a time. Code is not magically aware of things that are changing in your program, it cannot stop and wait for something to happen unless you tell it to. Unfortunately you cannot simply pause at this line in the code or else your program will stop responding.

You need a way for your program to continually check the text field's value. The simplest way to do this is with an actionListener object, which will subscribe to the text field and will be automatically called by the text field when something happens to it.

SpacePrez
  • 1,086
  • 7
  • 15