0

I have a method:

    private void jTextField5ActionPerformed(java.awt.event.ActionEvent evt) {                                            

}  

Whenever a user enters a particular character such as !, I want to gray out other textboxes. Is this possible within the above method, or can you only perform actions with buttons?

lord12
  • 2,837
  • 9
  • 35
  • 47

1 Answers1

0

You want a document listener, the below code should work. I would just check the text every time it changes, it saves navigating the DocumentEvent, there shouldn't be a noticeable difference in efficiency.

// Listen for changes in the text
textField.getDocument().addDocumentListener(new DocumentListener() {
  public void changedUpdate(DocumentEvent e) {
    updateField();
  }
  public void removeUpdate(DocumentEvent e) {
    updateField();
  }
  public void insertUpdate(DocumentEvent e) {
    updateField();
  }

  public void updateField() {
      if(textField.getText().indexOf("!")==-!)
      {
          //Doesn't have !
      }
      else
      {
          //Does have !
      }
  }
});

Taken from: Value Change Listener to JTextField

Community
  • 1
  • 1
Robadob
  • 5,319
  • 2
  • 23
  • 32