SWT:删除复合中不可见的组件空间



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

官方SWT示例的片段313应该可以帮助您。您基本上在 composite3 上设置了不同的FormData,具体取决于您是要显示还是隐藏它。

我有一个可观察的数据绑定,但它假设控件在 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;
        }
    }
}
        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);

这就解决了问题!:)

最新更新