Eclipse插件:显示带有可滚动文本的窗口

时间:2015-12-04 10:31:59

标签: java eclipse-plugin swt

在eclipse插件中,我尝试向用户显示一个只包含长文本的对话框。这个文本应该是可滚动的。

我试过了:

protected Control createDialogArea(Composite parent)
 {
    Composite container = (Composite) super.createDialogArea(parent);
    Text text = new Text(container, SWT.READ_ONLY | SWT.V_SCROLL | SWT.H_SCROLL| SWT.MULTI);

    text.setText("   " + command + "\n\r\n\r" + result);

    return container;
 }

然后使用禁用的滚动条显示文本(尽管它大于窗口的大小)。如何启用滚动?

2 个答案:

答案 0 :(得分:1)

问题似乎是,您对文本的布局数据不受限制。所以SWT似乎不知道何时启用滚动。

设置griddata以填充两者对我的代码不起作用(刚试过)。

然而,这将:

public static void main(String[] args) {

    Display display = Display.getDefault();
    Shell s = new Shell(display);

    s.setSize(300, 300);
    s.setLayout(new GridLayout(1, true));

    Composite c = new Composite(s, SWT.NONE);
    c.setLayout(new GridLayout(1, false));
    Text text = new Text(c, SWT.H_SCROLL | SWT.V_SCROLL | SWT.READ_ONLY);
    GridData gridData = new GridData(SWT.NONE, SWT.NONE, false, false);
    gridData.heightHint = 200;
    gridData.widthHint = 200;
    text.setLayoutData(gridData);

    text.setBackground(s.getDisplay().getSystemColor(SWT.COLOR_WHITE));

    text.setSize(250, 250);

    Font stdFont = new Font(text.getDisplay(), new FontData("Consolas", 11, SWT.NORMAL));

    text.setFont(stdFont);

    StringBuffer buffer = new StringBuffer();

    for (int row = 0; row < 40; row++) {
        for (int column = 0; column < 20; column++) {
            buffer.append("Word ");
        }
        buffer.append("\n");
    }

    text.setText(buffer.toString());

    s.open();



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

}

通过正确限制文本的大小(使用layoutdata,而不是设置大小),SWT现在知道文本何时大于区域并启用滚动。

请注意,如果您在创建后键入内容(我知道您的案例不可能),您的解决方案确实有效。

答案 1 :(得分:0)

如果您也设置了

,我也会工作
text.setLayoutData(new GridData(GridData.FILL_BOTH));