How can i have a disabled Text that is scrollable?

 ̄綄美尐妖づ 提交于 2019-12-12 09:56:50

问题


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.

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


回答1:


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:



来源:https://stackoverflow.com/questions/22166884/how-can-i-have-a-disabled-text-that-is-scrollable

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