Adding an Input Prompt to a text control is most common in web applications. Today I happened to see a JavaScript that handles this elegantly. The script itself is just 18 lines and using it requires just 1 line of code. What would be the effort required to create something in SWT? hmmm not very different:
public static void addPrompt(final Text text, final String defaultText) {
text.addFocusListener(new FocusListener() {public void focusGained(FocusEvent e) {
if(text.getText().equals(defaultText)) {
text.setText("");
text.setForeground(null);
}
}public void focusLost(FocusEvent e) {
if(text.getText().equals("")) {
text.setText(defaultText);
text.setForeground(text.getDisplay().getSystemColor(SWT.COLOR_GRAY));
}
}
});
}
To use:
InputPrompter.addPrompt(text, "Click Me!");
The result
The same trick can be applied to Combo boxes as well.
0 comments:
Post a Comment