2012-07-06 66 views
1

我在一個composite0中放置了三個複合(1,2,3),因此它們都根據composite0.setLayout(new FormLayout())設置了它們的佈局。現在我遇到的問題是,我將composite3隱藏起來:composite3.setVisible(false);(我不想刪除數據,仍然需要該組件,但不希望它顯示在UI上),現在componite2之後存在很大差距。如何消除較大的差距(用於放置composite2)?先進的謝謝你!SWT:刪除複合中的不可見組件空間

回答

-2
 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); 

這個問題解決了! :)

+1

嗨,你可以請加強這個代碼片段對於那些不知道你的代碼的人來說並沒有太大的好處,例如類'EditorPanel',方法'addSectionFormData()'和變量'a','b'和'C' – 2013-02-19 19:58:59

1

Snippet 313官方的SWT例子應該可以幫到你。您基本上在composite3上設置了不同的FormData,具體取決於您是要顯示還是隱藏它。

+0

您好,感謝您的回覆!我有問題,如果我將FormData設置爲composite1和composite2,然後刪除整個composite0.setLayout(新的FormLayout())的東西,那麼整個composite0將不會顯示在用戶界面上:( – huao 2012-07-06 18:15:34

0

我有一個數據綁定可觀察的,但它假定控件在一個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; 
     } 
    } 
}