Adding an input prompt to SWT Text
Posted On Jan 9, 2009 at by Prakash G.R.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.
Tired of unwanted and irrelevant results in Google? Try Eclipse Search, the Customized Seach Engine that gives you only relevant results - Powered by Google! It has plugins for Eclipse and Firefox/IE