-1

I have a button and a JTextField and if the JTextField is empty a message window opens informing that the field is empty. Now I want when I enter a number inside the JTextField, the text on the button to change. But I don't know which code to use.

I used this code

               tfInputTinter.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        btObserving.setText("Stop Observing");
                    }
                });

but nothing happens with the text on the button. Does anybody have an idea what should I do? Thanks

user3186565
  • 9
  • 1
  • 3

2 Answers2

1

I would say, this will do the trick. https://stackoverflow.com/a/3953219/3178834
The person in this post had the same problem I think.

Greetz, xwavex

Community
  • 1
  • 1
XWaveX
  • 268
  • 2
  • 11
  • +1, Yes a DocumentListener is the better approach. See the section from the Swing tutorial on [How to Write a Document Listener](http://docs.oracle.com/javase/tutorial/uiswing/events/documentlistener.html) for more information. – camickr Jan 12 '14 at 23:57
-1

You are wanting a KeyListener to listen for events on the keyboard.

        textField.addKeyListener(new KeyListener()
        {
            public void keyTyped(KeyEvent e)
            {
                // TODO Auto-generated method stub
            }
            public void keyReleased(KeyEvent e)
            {
                // TODO Auto-generated method stub
            }
            public void keyPressed(KeyEvent e)
            {
                // TODO Auto-generated method stub
            }
        });

Or rather than using the inner type way of working you can implement KeyListener in a class and implement the same set of methods in your class.

Chris
  • 171
  • 1
  • 7