2013-07-07 16 views
16

Bir kompozit (ve içindeki tüm çocuklar) gizlemem gerekiyor. Sadece setVisible(false) ayarı, kompozitin yerini tutacaktır.Bir SWT kompozitini boşluk kalmayacak şekilde nasıl gizlerim?

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

Çözüm http benzer : //stackoverflow.com/questions/17511442/eclipse-plugin-make-co mbo-to-handle-enter-key yani addListener() –

cevap

18

İstediğiniz şeyi yapan bazı kodlar.

enter image description here

gizleme sonra::

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

önce saklanarak: Ben temelde göstermek/gizlemek için Control#setVisible(boolean) birlikte CompositeGridData#exclude kullanmak

enter image description here

+0

sorunu shell.pack() ile, kullanamıyorum çünkü tüm gui'yi etkiler çünkü – yuris

+0

@yuris ebeveynde 'pack()' kullanabilir saklamak istediğin kompozit? Öyleyse güncellenmiş koduma bakın. Aksi takdirde, kabuğun boyutunu gizli bileşenin boyutuna göre elle azaltmak zorunda kalacaksınız. – Baz

+2

Nasıl content.getParent(). Düzeni (true, true) 'hakkında? – winklerrr

İlgili konular