2013-07-07 23 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 ie addListener() –

答えて

18

ここでは、必要な機能を実行するコードを示します。

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(); 
} 

隠ぺいする前に:私は基本的にCompositeを再表示/非表示にするControl#setVisible(boolean)と組み合わせてGridData#excludeを使用隠れた後

enter image description here

enter image description here

+0

問題はshell.pack()であり、GUI全体に影響するため使用できません – yuris

+0

@yuris親に 'pack()'を使用できますか?あなたが隠そうとしているコンポジット?もしそうなら、私の更新されたコードを見てください。そうでない場合は、隠しコンポジットのサイズ分だけシェルのサイズを手動で小さくする必要があります。 – Baz

+2

'content.getParent()。layout(true、true)'はどうですか? – winklerrr

関連する問題