user2179627
user2179627

Reputation: 381

Add SWT widget Group when a checkbox is selected

I have a GUI which has 3 different sections. In one of the section I have 2 checkboxes. I want to add one more widget multiple selection List when one of the checkbox is selected.

I added a selectionListener to checkbox and when it is selected I am calling method which creates multiple selection List.

Problem is this list is not getting added in the GUI when checkbox is selected and it is not removed when checkbox is unchecked.

I am not able to find the cause. Can anybody help me?

Below is the code to add the multiple selection list

private void createFirstLevelSubFolderGroup() {
    GridData gridData = new GridData();
    gridData.grabExcessVerticalSpace = true;
    gridData.verticalAlignment = org.eclipse.swt.layout.GridData.FILL;
    gridData.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
    gridData.grabExcessHorizontalSpace = true;
    subFolderGroup = new Group(this, SWT.NONE);        
    subFolderGroup.setLayout(new FillLayout());
    subFolderGroup.setLayoutData(gridData);
    subFolderGroup.setText("First Level SubFolder");           
    firstLevelFolderList = new List(subFolderGroup, SWT.V_SCROLL | SWT.MULTI);  
    subFolderGroup.setVisible(true);
    //firstLevelFolderList.setVisible(false);

 } 

 //code where the call to add this list is there
 //code adding a checkbox is here  and below am adding a listener to that checkbox
 ArchiveCheckbox.addSelectionListener(new SelectionAdapter()
    {
        @Override
        public void widgetSelected(SelectionEvent e)
        {
           if (ArchiveCheckbox.getSelection()) {  
           // here am trying to call the method which adds multiple selection list
           createFirstLevelSubFolderGroup();                                   
          }         
          else {
            // I want to remove that widget
           subFolderGroup.setVisible(false);                    
           firstLevelFolderList.removeAll();                    
         }
      }
    }

Basically I am not able to add this dynamically.

A small code snippet which demonstrates my scenario is fine if the code which I provided doesn't have the req info.

![Below is the gui which I am creating. When check box prepare archive for catch is selected I want a multiple selection list should appear just below the checkbox and it should disappear when it is unchecked. Currently when it is checked multiple selection list appearing but it hides the other group Model that contains sources ][1]

[2]: https://i.sstatic.net/Xl2ml.png

Upvotes: 0

Views: 1352

Answers (1)

greg-449
greg-449

Reputation: 111142

Create all the controls at the start. Set a GridData layout on each control you want to hide and set the exclude flag to true and make in control invisible. So something like:

Control control = .. create control ...

GridData data = new GridData(flags);
data.exclude = true;
control.setLayoutData(data);

control.setVisible(false);

When you want to make the controls visible set the exclude flag to false, make the control visible and call layout() on the parent Composite.

GridData data = (GridData)control.getLayoutData();
data.exclude = false;

control.setVisible(true);

parentComposite.layout();

Upvotes: 1

Related Questions