How to hide a Composite
To hide a composite managed with GridLayout, set visible to false, set the composite’s GridData.exclude to true, and force parent layout:
public void headerVisible(boolean visible){
header.setVisible(visible); // header is the composite that may need to be hidden
GridData gridData = (GridData) header.getLayoutData();
gridData.exclude = !visible; // If visible, exclude is false; if hidden, exclude is true.
parent.layout(true); //force parent layout
}
Updating a composite after widgets have been added or removed
Under GridLayout:
- a call to pack() will show the new widget, but will shrink the size of the composite and its contents.
- a call to layout() or layout(true) will do absolutly nothing–contrary to what the JavaDocs might say.
- only layout(true,true) will refresh the content of the composite without shrinking the size of the composite itself.
Note in the following snippet that adding a widget requires setting the layout data for the new widget.
private void addToComposite(Address address) {
AddressDisplayComposite adc = new AddressDisplayComposite(addressComposite, SWT.NONE);
adc.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
adc.setAddress(address); // initializes the composite using the address object
this.addressComposite.layout(true, true);
}