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