Search code examples
javaswtjfacewizard

Unable to redraw SWT Composite in a Wizard Page


I have a composite, which has 3 UI controls - SWT Group, SWT TabFolder, and a child composite that has a label and a link. In some use-cases the SWT Group is hidden, and I would like that the blank space is not seen instead the composite has to be redrawn and show only the SWT TabFolder and the child composite, without any space. I'm using composite.layout(true), but still there is blank space of the SWT Group when it is made hidden. Could someone help me solve this issue

TestComposite extends Composite{
   private Label childlabel;    
   private Group group;
   private TabFolder tabFolder;
   private Button button;

    TestComposite(Compossite parent){
       super(parent, SWT.NONE);
       setLayout(new GridLayout(1, true));
       setFont(parent.getFont());
       setBackground(parent.getBackground());
       GridDataFactory.fillDefaults().grab(true, true).applyTo(this);
       createControl();
    }

    public void createControl() {

    this.group = new Group(this, SWT.SHADOW_IN);       
    this.group.setSize(this.getDisplay().getActiveShell().getSize());        
    this.button = new Button(this.group, SWT.PUSH); 
    GridDataFactory.swtDefaults().applyTo(this.button);
         
    this.tabFolder = new TabFolder(this, SWT.TOP);
    GridDataFactory.swtDefaults().align(SWT.FILL, SWT.FILL).grab(true, 
    true).applyTo(this.tabFolder);

    Composite childComposite = new Composite(this, SWT.NONE);
    childComposite .setLayout(new GridLayout(2, false));
    GridDataFactory.swtDefaults().grab(true, false).align(SWT.LEFT, 
    SWT.TOP).applyTo(childComposite );
    this.childLabel = new Label(childComposite, SWT.NONE);
    GridDataFactory.swtDefaults().grab(true, false).applyTo(childlabel);

    setInput(abc);
    enableVisibilityOfGroup(false);
}

public void enableVisibilityOfGroup(Boolean visible){
    this.group.setVisible(visible);
    // this shoudl redraw the UI and shown only tabFolder and label in the
    // whole wizard page, but there is blank space in place of group
    this.layout(true);
}
    

}

Solution

  • Set GridData on the Group

    GridDataFactory.swtDefaults().applyTo(this.group);
    

    Use GridData.exclude to exclude/include the group in the layout:

    public void enableVisibilityOfGroup(boolean visible) {
        this.group.setVisible(visible);
    
        GridData gridData = (GridData)this.group.getLayoutData();
        gridData.exclude = !visible;
    
        this.layout(true);
    }