Laishram Krishnananda Singh Laishram Krishnananda Singh - 4 months ago 15
Java Question

Conflict between VerifyListener and FocusListener

There is a SWT text box. I am setting a default text on it.


  1. A focus listener is set so that when the focus is on the text box, the default text is deleted.

  2. A verify listener is set to make sure only alphabet is entered.



However, the text is not set to empty when focus is set on the text box. What is causing this problem?

final Text text = new Text(parent, SWT.NONE);
text.setText("Default");
text.addFocusListener( new FocusAdapter() {
@Override
public void focusGained(FocusEvent event) {
if("Default".equals(text.getText())){
text.setText("");
}
}
});
text.addVerifyListener( new VerifyListener() {
@Override
public void verifyText(VerifyEvent e) {
e.doit = Verifier.isAlphabet(e.character);
}
});

Answer

Text already has support for default text via the method setMessage.

So, instead of using setText("Default") and the focus listener you could just use setMessage("Default");.

The verify listener should work correctly with it.