如何动态调整 TitleAreaDialog 中控件的大小

how dynamically adjust size of controls in TitleAreaDialog

我的 TitleAreaDialog 可以有 n 棵树,每棵树都有自己的组合。如果我只有 2 棵树,对话框看起来不错,但如果树的数量增加,树的内容就会被截断。

我想我漏掉了一些小东西..

代码如下:

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TreeItem;


public class testTitleDialog extends TitleAreaDialog {

private static final String DIALOG_TITLE = "TITLE AREA DIALOG";

Composite area;

public testTitleDialog(Shell parentShell) {
    super(parentShell);
}

@Override
public void create() {
    super.create();
    setTitle("TITLE WILL BE HERE");
}

@Override
protected Control createDialogArea(final Composite parent) {
    this.area = (Composite) super.createDialogArea(parent);
    parent.getShell().setText(DIALOG_TITLE);
    Composite area = (Composite) super.createDialogArea(parent);

    Composite contents = (Composite) super.createDialogArea(area);
    contents.setLayout(new GridLayout());
    contents.setLayoutData(new GridData(GridData.FILL_BOTH));
    contents.addDisposeListener(new DisposeListener(){
        public void widgetDisposed(DisposeEvent e) {
        }
    });
    createMapperComposite(contents);
    return this.dialogArea;
}

private void createMapperComposite(Composite composite) {
    Composite main = new Composite(composite, SWT.NONE);
    main.setLayout(new GridLayout());
    main.setLayoutData(new GridData(GridData.FILL_BOTH));

    treeMapper(main);

}

private Composite treeMapper(Composite main) {
    SashForm sashForm = new SashForm(main, SWT.HORIZONTAL);

    Composite composite = new Composite(sashForm, SWT.NONE);
    composite.setLayout(new GridLayout());
    composite.setLayoutData(new GridData(GridData.FILL_BOTH));
    composite.setBackground(ColorConstants.white);


    //i is number of trees   
    for (int i = 0; i < 10; i++) {

        Label lbl = new Label(composite, SWT.NONE);
        lbl.setText("Tree " + i);

        // Add content to scrolled composite
        Composite scrolledContent = new Composite(composite, SWT.NONE);
        scrolledContent.setLayout(new GridLayout());
        GridData gridData = new GridData();
        gridData.horizontalAlignment = SWT.FILL;
        gridData.grabExcessHorizontalSpace = true;
        gridData.verticalAlignment = SWT.FILL;
        gridData.grabExcessVerticalSpace = true;
        scrolledContent.setLayoutData(gridData);

        final TreeViewer tree = new TreeViewer(scrolledContent);
        for(int loopIndex0 = 0; loopIndex0 < 10; loopIndex0++) {
            TreeItem treeItem0 = new TreeItem(tree.getTree(), 0);
            treeItem0.setText("Level 0 Item "+ loopIndex0);

            for(int loopIndex1 = 0; loopIndex1 < 10; loopIndex1++) {
                TreeItem treeItem1 = new TreeItem(treeItem0, 0);
                treeItem1.setText("Level 1 Item "+ loopIndex1);

                for(int loopIndex2 = 0; loopIndex2 < 10; loopIndex2++) {
                    TreeItem treeItem2 = new TreeItem(treeItem1, 0);
                    treeItem2.setText("Level 2 Item "+ loopIndex2);
                }
            }
        }

        tree.getTree().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));


    }

    new TreeViewer(sashForm);
    return main;
}

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

@Override
protected void okPressed() {
    super.okPressed();
}



public static void main(final String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    new testTitleDialog(shell).open();
}

}

您没有采取任何措施来处理显示的对话框太大的问题。你必须使用像 ScrolledComposite 这样的东西来处理这个:

@Override
protected Control createDialogArea(final Composite parent) {
  Composite area = (Composite) super.createDialogArea(parent);
  parent.getShell().setText(DIALOG_TITLE);

  ScrolledComposite scrolled = new ScrolledComposite(area, SWT.V_SCROLL | SWT.H_SCROLL);

  GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
  data.heightHint = 500;
  scrolled.setLayoutData(data);

  scrolled.setExpandHorizontal(true);
  scrolled.setExpandVertical(true);

  createMapperComposite(scrolled);

  return area;
}

private void createMapperComposite(ScrolledComposite composite) {
  Composite main = new Composite(composite, SWT.NONE);
  main.setLayout(new GridLayout());
  main.setLayoutData(new GridData(GridData.FILL_BOTH));

  treeMapper(main);

  composite.setContent(main);
  composite.setMinSize(main.computeSize(SWT.DEFAULT, SWT.DEFAULT));
}

注意:您必须为对话框选择一个高度(请参阅 data.heightHint = 500)才能使其正常工作。

您的代码调用了 super.createDialogArea 三次 次 - 这必须恰好调用一次。您还 returned this.dialogArea - 您应该 return return 由 super.createDialogArea 编辑的值。