Laishram Krishnananda Singh Laishram Krishnananda Singh - 1 year ago 116
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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download