我怎样才能有一个可滚动的禁用文本? [英] How can i have a disabled Text that is scrollable?

查看:189
本文介绍了我怎样才能有一个可滚动的禁用文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的文字声明为,

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

在某些情况下应该禁用它。但是当我做

text.setEnabled(false); 文本的滚动条也被禁用,我无法完全看到文本中的值。

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.

我知道Text中的setEditable()方法,但我想要与Text相同的行为被禁用,即背景颜色改变,没有闪烁的光标(插入符号),无法进行鼠标点击和文本无法选择等。

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.

我能够更改背景做颜色

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?

推荐答案

你不会是能够使 Text 控件在禁用时显示滚动条。这就是本机控件的工作方式,即操作系统呈现控件的方式。

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.

但是,你可以包装你的文本 ScrolledComposite 中。这样, ScrolledComposite 将滚动而不是文本

However, you can wrap your Text in a ScrolledComposite. That way, the ScrolledComposite will scroll instead of the 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();
}

这就是它的样子:

这篇关于我怎样才能有一个可滚动的禁用文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆