我怎样才能拥有可滚动的禁用文本?

Mon*_*kka 2 java swt scroll textbox disabled-control

我的文本声明为,

Text text= new Text(parent, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL | SWT.WRAP);
Run Code Online (Sandbox Code Playgroud)

在某些情况下应该禁用它。但是当我这样做时
text.setEnabled(false); 文本的滚动条也被禁用,我无法完全看到文本中的值。

我的文本字段不能为只读。在某些情况下它应该是可编辑的。

我知道文本中的 setEditable() 方法,但我希望具有与禁用文本时相同的行为,即背景颜色更改、不闪烁光标(插入符号)、无法单击鼠标并且文本不可选择ETC。

我可以通过以下方式更改背景颜色

text.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
Run Code Online (Sandbox Code Playgroud)

但我无法禁用光标、文本选择和鼠标单击。

在此输入图像描述

有没有办法使禁用文本的滚动条保持活动状态?

Baz*_*Baz 5

Text禁用后,您将无法使控件显示滚动条。这只是本机控件的工作方式,即操作系统呈现控件的方式。

但是,您可以将您的内容包装TextScrolledComposite. 这样,ScrolledComposite将滚动而不是Text

这是一个例子:

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();
}
Run Code Online (Sandbox Code Playgroud)

它看起来是这样的:

在此输入图像描述