3

I would like the user to enter a value into the JTextField and use a listener to listen to the textfield and print the value to the console straightaway without pressing a key.

textfield1.addChangeListener(new ChangeListener() {
    public void actionPerformed(ActionEvent e) {                
        System.out.println(textfield1);
    }
});

error:

<anonymous textfield$2> is not abstract and does not override abstract method stateChanged(ChangeEvent) in ChangeListener
Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97
user2994263
  • 161
  • 1
  • 6
  • 14
  • Possible duplication of http://stackoverflow.com/questions/3953208/value-change-listener-to-jtextfield – maheeka Dec 09 '13 at 23:25
  • Iam talking about ChangeListener not documentlistener. Could you please help me? – user2994263 Dec 09 '13 at 23:36
  • I don't think you will be, but please comment on my answer if you're confused about where I'm telling you to put the code. It's very easy though; you'll probably get it. – Michael Yaworski Dec 09 '13 at 23:43
  • Consider the KeyListener instead of the ChangeListener – Anto Dec 09 '13 at 23:51
  • @user2994263, `Iam talking about ChangeListener not documentlistener` - you should NOT be using a ChangeListener. You SHOULD be using a DocumentListener. – camickr Dec 10 '13 at 02:23
  • @mikeyaworski `Please accept an answer` - using a KeyListener is not the best answer. The best answer is the comment that suggests to use a DocumentListener. A KeyListener won't work if the user uses DnD to drop text to the text field, or if the application has a `Paste` menu item. Rarely would you use a KeyListener with Swing. Swing has newer and better API's than AWT. – camickr Dec 10 '13 at 02:25
  • @camickr well the OP said he didn't want to use a DocumentListener, so the two answers have now included two other forms of a KeyListener. For what he's wanting to do, anything suggested will work and he hasn't even left a comment. – Michael Yaworski Dec 10 '13 at 02:35
  • @mikeyaworski, `anything suggested will work` - I just gave you two examples when it will NOT work. – camickr Dec 10 '13 at 02:44

3 Answers3

3

Yes, just use the KeyListener class, see the example below:

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class Main extends JFrame {
  public Main() throws HeadlessException {
    setSize(200, 200);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new FlowLayout(FlowLayout.LEFT));

    JLabel label = new JLabel("Write something: ");
    JTextField input = new JTextField();

    input.setPreferredSize(new Dimension(100, 20));

    final JTextField output = new JTextField();
    output.setPreferredSize(new Dimension(100, 20));
    add(label);
    add(input);
    add(output);

    input.addKeyListener(new KeyAdapter() {
        public void keyReleased(KeyEvent e) {
            JTextField textField = (JTextField) e.getSource();
            String text = textField.getText();
            output.setText(text);
        }

        public void keyTyped(KeyEvent e) {

        }

        public void keyPressed(KeyEvent e) {
        }
    });
}

public static void main(String[] args) {
    new Main().setVisible(true);
}
}
Anto
  • 4,265
  • 14
  • 63
  • 113
3

Put this private class into your public class. Just like a method.

private class textChangedListener implements KeyListener 
{
    public void keyPressed(KeyEvent e){} 
    public void keyReleased(KeyEvent e){}

    public void keyTyped(KeyEvent e) 
    {
        System.out.print(textField1.getText());
    } 
}

And then call it to your JTextField in your main method like so:

private JTextField textField1; // just showing the name of the JTextField
textField1.addKeyListener(new textChangedListener());
Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97
1

You can set a addlistener to textproperty of the text field.

textField.textProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
                System.out.println("textfield changed to "+ newValue);
            }
        });
Jamith NImantha
  • 1,999
  • 2
  • 20
  • 27