Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java equivalent to C# TextBox TextChanged event

in C# there is an event for textboxes as follows

private void fooText_TextChanged(object sender, EventArgs e)
{
    //do something 
}

the code in the fooText_TextChanged is fired once the text within the textbox is altered.

What is the java equivalent to this? Or how can something similar to this be achieved in java?

Thanks for any feedback/help/advice.

like image 662
Ari Avatar asked Jan 04 '12 02:01

Ari


2 Answers

For Swing, If you wanted to be notified after the text component's text had changed, you'd use a DocumentListener that was added to the JTextComponent's Document. e.g.,

  JTextField myField = new JTextField();

  myField.getDocument().addDocumentListener(new DocumentListener() {

     public void removeUpdate(DocumentEvent e) {
        // TODO add code!

     }

     public void insertUpdate(DocumentEvent e) {
        // TODO add code!

     }

     public void changedUpdate(DocumentEvent e) {
        // TODO add code!

     }
  });

If on the other hand, you wanted to check text before it has been committed to the text component, you'd add a DocumentFilter to the JTextComponent's Document.

like image 50
Hovercraft Full Of Eels Avatar answered Sep 28 '22 15:09

Hovercraft Full Of Eels


I recommend registering a DocumentListener to your component's document. Therein, you'll listen for DocumentEvents.

like image 24
mre Avatar answered Sep 28 '22 15:09

mre