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.addFocusListener( new FocusAdapter() {
public void focusGained(FocusEvent event) {
text.addVerifyListener( new VerifyListener() {
public void verifyText(VerifyEvent e) {
e.doit = Verifier.isAlphabet(e.character);

Answer Source

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.