Reputation: 87
There is a SWT text box. I am setting a default text on it.
A focus listener is set so that when the focus is on the text box, the default text is deleted.
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);
}
});
Upvotes: 0
Views: 60
Reputation: 7638
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.
Upvotes: 2
Reputation: 111142
When your focus listener calls text.setText("")
the verify listener is called. In this case the value of e.character
is 0 because no character is being added. Presumably your Verifier.isAlphabet
method is returning false
for this and stopping the change from happening.
Your verify listener should not be testing the character
value. You should be looking at the VerifyEvent
text
field which gives you the full text being changed (you may also need to look at the start
and end
fields).
Upvotes: 0