2013-07-07 42 views
16

我需要隱藏複合(以及所有的孩子)。只需設置setVisible(false)將保留組合的空間。如何隱藏SWT組合以免佔用空間?

Composite outer = new Composite(parent, SWT.NONE);  
outer.setLayout(new GridLayout(1,false)); 
outer.setLayoutData(new GridData(GridData.FILL_BOTH)); 

Composite compToHide = new MyComposite(outer, SWT.NONE);   
compToHide.setLayout(new GridLayout()); 
compToHide.setVisible(false); 
+0

解決方案是類似於HTTP ://stackoverflow.com/questions/17511442/eclipse-plugin-make-co mbo-to-handle-enter-key即addListener() –

回答

18

這是一些代碼,可以做你想做的。我的組合基本上是用GridData#excludeControl#setVisible(boolean)隱藏/取消隱藏Composite

public static void main(String[] args) 
{ 
    Display display = new Display(); 
    final Shell shell = new Shell(display); 
    shell.setText("StackOverflow"); 
    shell.setLayout(new GridLayout(1, true)); 

    Button hideButton = new Button(shell, SWT.PUSH); 
    hideButton.setText("Toggle"); 

    final Composite content = new Composite(shell, SWT.NONE); 
    content.setLayout(new GridLayout(3, false)); 

    final GridData data = new GridData(SWT.FILL, SWT.FILL, true, true); 
    content.setLayoutData(data); 

    for(int i = 0; i < 10; i++) 
    { 
     new Label(content, SWT.NONE).setText("Label " + i); 
    } 

    hideButton.addListener(SWT.Selection, new Listener() 
    { 
     @Override 
     public void handleEvent(Event arg0) 
     { 
      data.exclude = !data.exclude; 
      content.setVisible(!data.exclude); 
      content.getParent().pack(); 
     } 
    }); 

    shell.pack(); 
    shell.open(); 
    while (!shell.isDisposed()) 
    { 
     if (!display.readAndDispatch()) 
      display.sleep(); 
    } 
    display.dispose(); 
} 

之前隱藏:

enter image description here

隱藏後:

enter image description here

+0

問題與shell.pack(),我不能使用它,因爲它影響整個gui – yuris

+0

@yuris你可以使用'pack()'的父你試圖隱藏的組合?如果是這樣,看看我更新的代碼。如果不是,您將不得不手動減小隱藏複合大小的外殼大小。 – Baz

+2

'content.getParent()。layout(true,true)'如何? – winklerrr