Selected color of prompt/message in empty text box

限于喜欢 提交于 2019-12-24 07:38:44

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!