1

我在一个composite0中放了三个composite(1,2,3),所以它们都基于 composite0.setLayout(new FormLayout()). 我现在遇到的问题是,我使composite3不可见:(composite3.setVisible(false);我不想删除数据,仍然需要那个组件,但只是不希望它显示在UI上),现在componite2之后有很大的差距。如何消除大间隙(用于放置复合材料2)?提前谢谢你!

4

3 回答 3

1

我有一个可观察的数据绑定,但它假定控件位于 GridLayout 内。我使用它在向导页面中显示和隐藏组合和小部件,具体取决于复选框的选择状态。

要使用它,请使用以下内容进行设置:

DataBindingContext dbc = new DataBindingContext();
Button button = new Button(composite, SWT.CHECK);
IObservableValue target = SWTObservables.observeSelection(button);
dbc.bindValue(target, new HideControlObservable(someControl));

这是可观察的:

/**
 * Observable to control the presence (visibility and size) of any control
 * within a grid layout.
 * <p>
 * Changing the value of this observable will do two things:
 * <ol>
 * <li>Set the visibility of the control</li>
 * <li>Set the size of the control to zero when the control is invisible.</li>
 * </ol>
 * So, when using this observable, the control will not only be invisible,
 * but it will be gone, completely. Normally, when setting the visibility of
 * a control to <code>false</code>, the control will not be displayed but
 * will still take all the space on the screen.
 * </p>
 * <p>
 * <strong>Note:</strong> this observable works for controls within a
 * <strong>GridLayout only</strong>.
 * </p>
 */
public class HideControlObservable extends WritableValue implements IValueChangeListener {
    private final DataBindingContext dbc = new DataBindingContext();
    private final ISWTObservableValue sizeObservable;
    private final Point size = new Point(0, 0);
    private final Control control;

    public HideControlObservable(Control control) {
        super(control.getVisible(), Boolean.class);
        this.control = control;

        UpdateValueStrategy never = new UpdateValueStrategy(UpdateValueStrategy.POLICY_NEVER);
        dbc.bindValue(SWTObservables.observeVisible(control), this, never, null);

        sizeObservable = SWTObservables.observeSize(control);
        sizeObservable.addValueChangeListener(this);

        if (!control.isVisible()) {
            GridData gd = (GridData) control.getLayoutData();
            if (gd == null) {
                gd = new GridData();
            }
            gd.exclude = true;
            control.setLayoutData(gd);
            control.setSize(new Point(0, 0));
        }
    }

    @Override
    public void doSetValue(Object value) {
        super.doSetValue(value);
        Boolean bool = (Boolean) value;
        if (bool) {
            GridData gd = (GridData) control.getLayoutData();
            if (gd == null) {
                gd = new GridData();
            }
            gd.exclude = false;
            control.setLayoutData(gd);
            control.setSize(size);
            control.getParent().layout();
        } else {
            GridData gd = (GridData) control.getLayoutData();
            if (gd == null) {
                gd = new GridData();
            }
            gd.exclude = true;
            control.setLayoutData(gd);
            control.setSize(new Point(0, 0));
            control.getParent().layout();
        }
    }

    @Override
    public synchronized void dispose() {
        sizeObservable.dispose();
        super.dispose();
    }

    @Override
    public void handleValueChange(ValueChangeEvent event) {
        Point newSize = (Point) event.getObservableValue().getValue();
        if (newSize.x > size.x) {
            size.x = newSize.x;
        }
        if (newSize.y > size.y) {
            size.y = newSize.y;
        }
    }
}
于 2013-01-31T14:37:51.450 回答
1

官方 SWT 示例的片段 313应该可以帮助您。您基本上FormData根据您是要显示还是隐藏它来设置不同的 Composite3。

于 2012-07-06T07:27:20.750 回答
-3
        FormData data = new FormData();
        data.left = new FormAttachment(0, 100, EditorPanel.SPACING);
        data.top = new FormAttachment(section1, EditorPanel.SPACING);
        data.height = 0;
        data.width = 0;
        addSectionFormData(a, b, c);
        a.setLayoutData(data);
        b.setLayoutData(data);

这解决了问题!:)

于 2012-07-09T21:24:27.227 回答