Eclipse RCP对话框:大文本框不会滚动 - 而是创建一个巨大的对话窗口!

时间:2010-07-05 13:55:08

标签: java layout dialog eclipse-rcp

我一直在敲打这一段时间,我似乎无法到达任何地方。我已经尝试了所有可以在网上找到的例子,但似乎没有任何效果!我一直无法找到这个问题,这让我觉得我错过了一些基本的东西。 。

在我的Eclipse RCP程序中,我想显示一个对话框,显示加载数据文件时发生的错误列表。我已经重写了TitleAreaDialog,只想显示一个包含错误列表和OK按钮的可滚动文本。

问题是Text垂直滚动条不会变为活动状态 - Text会变得更高以适应文本。这使对话框窗口高度增加,直到它适合文本框或直到它达到屏幕的高度 - 然后它只是切断文本框的底部。

如何防止对话框/文本框变得过大?我错过了什么?

感谢您的帮助!!
-Christine

...

这是一个显示我的对话框的简单程序:

import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.*;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;


public class ScrollableDialogRunner {

    public static void main(String[] args) {
        System.out.println("starting");
        Display display = new Display ();
        Shell shell = new Shell (display);

        String errors = "one\ntwo\nthree\nfour\nfive\n";
        for(int i = 0; i < 5; i++){
            errors += errors;
        }

        ScrollableDialog dialog = new ScrollableDialog(shell, "Errors occurred during load", "The following errors occurred while loaded file 'x.data'", errors);
        dialog.open();
    }
}


class ScrollableDialog extends TitleAreaDialog {
    private String title;
    private String text;
    private String scrollableText;

    public ScrollableDialog(Shell parentShell, String title, String text, String scrollableText) {
        super(parentShell);
        this.title = title;
        this.text = text;
        this.scrollableText = scrollableText;
    }

    @Override
    protected Control createDialogArea(Composite parent) {
        GridLayout layout = new GridLayout();
        layout.numColumns = 1;
        parent.setLayout(layout);

        GridData gridData = new GridData();
        gridData.grabExcessHorizontalSpace = true;
        gridData.horizontalAlignment = GridData.FILL;

        Text scrollable = new Text(parent, SWT.BORDER | SWT.V_SCROLL);
        scrollable.setLayoutData(gridData);
        scrollable.setText(scrollableText);

        return parent;
    }

    @Override
    public void create() {
        super.create();

        setTitle(title);
        setMessage(text, IMessageProvider.ERROR);
    }

    @Override
    protected void createButtonsForButtonBar(Composite parent) {
        Button okButton = createButton(parent, OK, "OK", true);
        okButton.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                close();
            }
        });
    }

    @Override
    protected boolean isResizable() {
        return false;
    }
}

1 个答案:

答案 0 :(得分:3)

为对话框指定大小;否则,对话框将布置孩子们询问他们的“首选”大小(对于文本小部件是无限的)并且将相应地调整自身大小。

[编辑]此版本有效。有关详细信息,请参阅我的评论。

class ScrollableDialog extends TitleAreaDialog {
    private String title;
    private String text;
    private String scrollableText;

    public ScrollableDialog(Shell parentShell, String title, String text, String scrollableText) {
        super(parentShell);
        this.title = title;
        this.text = text;
        this.scrollableText = scrollableText;
    }

    @Override
    protected Control createDialogArea(Composite parent) {
        Composite composite = (Composite) super.createDialogArea (parent); // Let the dialog create the parent composite

        GridData gridData = new GridData();
        gridData.grabExcessHorizontalSpace = true;
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessVerticalSpace = true; // Layout vertically, too! 
        gridData.verticalAlignment = GridData.FILL;

        Text scrollable = new Text(composite, SWT.BORDER | SWT.V_SCROLL);
        scrollable.setLayoutData(gridData);
        scrollable.setText(scrollableText);

        return composite;
    }

    @Override
    public void create() {
        super.create();

        // This is not necessary; the dialog will become bigger as the text grows but at the same time,
        // the user will be able to see all (or at least more) of the error message at once
        //getShell ().setSize (300, 300);
        setTitle(title);
        setMessage(text, IMessageProvider.ERROR);

    }

    @Override
    protected void createButtonsForButtonBar(Composite parent) {
        Button okButton = createButton(parent, OK, "OK", true);
        okButton.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                close();
            }
        });
    }

    @Override
    protected boolean isResizable() {
        return true; // Allow the user to change the dialog size!
    }
}