Question

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?

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top