SWT:扩展栏的宽度

时间:2013-12-05 20:44:53

标签: java eclipse widget swt jface

目前我正在玩SWT并尝试实施扩展栏:

    protected Control createDialogArea(Composite parent) {
            Composite container = (Composite) super.createDialogArea(parent);

            ExpandBar bar = new ExpandBar(container, SWT.V_SCROLL);

            Composite newConstraint = new Composite(bar, SWT.NONE);
            GridLayout gridLayout = new GridLayout();
            gridLayout.numColumns = 3;
            newConstraint.setLayout(gridLayout);

            Label sourcelbl = new Label(newConstraint, SWT.NONE);
            sourcelbl.setImage(getMyImage("source.png"));
            sourcelbl.setLayoutData(new GridData(GridData.CENTER, GridData.CENTER,
                    true, false));

            Label spacelbl = new Label(newConstraint, SWT.NONE);

            Label targetlbl = new Label(newConstraint, SWT.NONE);
            targetlbl.setImage(getMyImage("target.png"));
            targetlbl.setLayoutData(new GridData(GridData.CENTER, GridData.CENTER,
                    true, false));

            ListViewer lvSource = new ListViewer(newConstraint);
            lvSource.setContentProvider(new ConstraintDialogContentProvider());
            lvSource.setLabelProvider(new ConstraintDialogLabelProvider());
            lvSource.setInput(fm);

            Combo constrainType = new Combo(newConstraint, SWT.NONE);
            constrainType.setItems(new String[] { "require", "exclude" });
            constrainType.setLayoutData(new GridData(GridData.CENTER,
                    GridData.CENTER, true, false));

            ListViewer lvTarget = new ListViewer(newConstraint);
            lvTarget.setContentProvider(new ConstraintDialogContentProvider());
            lvTarget.setLabelProvider(new ConstraintDialogLabelProvider());
            lvTarget.setInput(fm);

            ExpandItem item0 = new ExpandItem(bar, SWT.NONE, 0);
            item0.setText("New Constraint");
            item0.setHeight(newConstraint.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
            item0.setControl(newConstraint);

omposite existingConstraints = new Composite(bar, SWT.NONE);
        gridLayout = new GridLayout();
        gridLayout.numColumns = 2;
        existingConstraints.setLayout(gridLayout);

            ........

            ExpandItem item1 = new ExpandItem(bar, SWT.NONE, 1);
            item1.setText("Existing Constraints");
            item1.setHeight(existingConstraints.computeSize(SWT.DEFAULT,
                    SWT.DEFAULT).y);
            item1.setControl(existingConstraints);

            item0.setExpanded(true);
            bar.setSpacing(8);

            return container;
        }

然后问题是宽度,似乎没有计算:

enter image description here

没有扩展栏,一切正常,宽度会自动计算:

enter image description here

如何让extendbar计算宽度???

干杯, 菲尔

2 个答案:

答案 0 :(得分:1)

ExpandBar大小计算似乎只显示ExpandItem文本的大小,并忽略项目中控件的大小。

您必须覆盖ExpandBar.computeSize方法才能更改此项(以及ExpandBar.checkSubclass以停止拒绝您的子类)。

或者您可以在文本末尾包含空格,以便ExpandItems捏造计算出的大小。

答案 1 :(得分:0)

根据greg-449的回答,我找到了另一种解决方法,即为ExpandBar设置widthHint:

    ExpandBar expandBar = new ExpandBar(this, SWT.BORDER);
    GridData gridDataExpandBar = new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1);
    gridDataExpandBar.widthHint = 150;