How can i have a disabled Text that is scrollable?

2019-05-24 15:49发布

My Text is declared as,

Text text= new Text(parent, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL | SWT.WRAP);

It should be disabled in some cases. But when I do
text.setEnabled(false); the text's scroll bar gets disabled too and i am not able to see the value in the Text completely.

My Text Field cannot be READ ONLY. It should be editable in some cases.

I am aware of setEditable() method in Text, but I would like to have the same behaviour as when the Text is disabled i.e, background color change, no blinking cursor(caret), unable to do mouse clicks and the text being not selectable etc.

I was able to change the background color by doing

text.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));

But i couldn't disable the cursor, text selection and the mouse click.

enter image description here

Is there a way to keep the scroll bar active for a disabled Text?

1条回答
神经病院院长
2楼-- · 2019-05-24 16:12

You won't be able to make the Text control show scrollbars when it'd disabled. It's just the way the native control works, i.e., the way the OS renders the controls.

However, you can wrap your Text in a ScrolledComposite. That way, the ScrolledComposite will scroll instead of the Text.

Here is an example:

public static void main(String[] args)
{
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout(SWT.VERTICAL));

    final ScrolledComposite composite = new ScrolledComposite(shell, SWT.V_SCROLL);
    composite.setLayout(new FillLayout());

    final Text text = new Text(composite, SWT.MULTI | SWT.BORDER | SWT.WRAP);

    composite.setContent(text);
    composite.setExpandHorizontal(true);
    composite.setExpandVertical(true);
    composite.setMinSize(text.computeSize(SWT.DEFAULT, SWT.DEFAULT));

    Button button = new Button(shell, SWT.PUSH);
    button.setText("Add text and disable");
    button.addListener(SWT.Selection, new Listener()
    {
        @Override
        public void handleEvent(Event arg0)
        {
            text.setText("lalala\nlalala\nlalala\nlalala\nlalala\nlalala\n");
            text.setEnabled(false);
            composite.setMinSize(text.computeSize(SWT.DEFAULT, SWT.DEFAULT));
        }
    });

    shell.pack();
    shell.setSize(300, 150);
    shell.open();

    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

This is what it looks like:

enter image description here

查看更多
登录 后发表回答