问题
I want to create Text box with prompt when it is empty. I use setMessage method and it's working fine. How can I change default color of prompt?
回答1:
If you want to change the color of the prompt, just add a Listener
to the focus events.
public class StackOverflow
{
public static void main(String[] args)
{
final Display display = new Display();
Shell shell = new Shell(display);
shell.setText("StackOverflow");
shell.setLayout(new GridLayout(1, true));
final Text text = new Text(shell, SWT.BORDER | SWT.SEARCH);
text.setForeground(display.getSystemColor(SWT.COLOR_RED));
text.setText("Enter something");
text.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, true));
text.addListener(SWT.FocusOut, new Listener()
{
@Override
public void handleEvent(Event arg0)
{
if("".equals(text.getText()))
{
text.setForeground(display.getSystemColor(SWT.COLOR_RED));
text.setText("Enter something");
}
}
});
text.addListener(SWT.FocusIn, new Listener()
{
@Override
public void handleEvent(Event arg0)
{
if("Enter something".equals(text.getText()))
{
text.setForeground(display.getSystemColor(SWT.COLOR_BLACK));
text.setText("");
}
}
});
Label label = new Label(shell, SWT.NONE);
label.setFocus();
label.forceFocus();
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Unfocussed/empty:

Focussed/not empty:

As you can see, the "prompt" is now red.
来源:https://stackoverflow.com/questions/13586772/selected-color-of-prompt-message-in-empty-text-box