Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
411 views
in Technique[技术] by (71.8m points)

resize - SWT Why is parent Composite resized after I create a new Composite in parent?

I want to recreate the functionality and look of ExpandItem. I am creating a Composite which has a StyledText widget and a Button. When the Button is pressed, a new Composite is created below these two, and when it is pressed again, this new Composite is hidden. However, after creating the new Composite, my parent Composite resizes for no reason. I am attaching my code and pictures of my work. I would appreciate any kind of help.

Shell shell = new Shell();
//    shell.setSize(400, 400);
Display display = shell.getDisplay();
shell.setLayout(new FillLayout());

//create outerComp
GridLayout gridLayoutOuterComp = new GridLayout();

GridData gridDataOuterComp = new GridData(SWT.FILL, SWT.FILL, true, true);
gridDataOuterComp.heightHint = 150;

Composite outerComp = new Composite(shell, SWT.NONE);
outerComp.setLayout(gridLayoutOuterComp);
outerComp.setLayoutData(gridDataOuterComp);

//create innerComp
GridLayout gridLayoutInnerComposite = new GridLayout();
gridLayoutInnerComposite.numColumns = 5;

GridData gridDataInnerComposite = new GridData(SWT.FILL, SWT.FILL, true, false);
gridDataInnerComposite.heightHint = 40;

Composite composite = new Composite(outerComp, SWT.NONE);
composite.setLayout(gridLayoutInnerComposite);
composite.setLayoutData(gridDataInnerComposite);
composite.setBackground(display.getSystemColor(SWT.COLOR_GREEN));

// create the styled text widget
GridData gridDataStyledText = new GridData();
gridDataStyledText.grabExcessHorizontalSpace = true;
gridDataStyledText.horizontalSpan = 4;
gridDataStyledText.horizontalAlignment = SWT.FILL;
gridDataStyledText.heightHint = 30;

StyledText widget = new StyledText(composite, SWT.BORDER);
String textNew = "This is StyledText in Composite";
widget.setText(textNew);
widget.setLayoutData(gridDataStyledText);

//create Button
GridData gridDataButton = new GridData();
gridDataStyledText.horizontalSpan = 1;
gridDataButton.heightHint = 30;
gridDataButton.grabExcessHorizontalSpace = false;
gridDataButton.horizontalAlignment = SWT.END;

Button button = new Button(composite, SWT.PUSH);
button.setText("B1");
button.setLayoutData(gridDataButton);

button.addSelectionListener(new SelectionListener() {
  @Override
  public void widgetSelected(SelectionEvent e) {
    GridData newCompGridData = new GridData();
    if (counter % 2 == 1) {
      if (newComp != null && !newComp.isVisible()) {
        newComp.setVisible(true);

        GridData newData = new GridData();
        newData.heightHint = 100;
        composite.setLayoutData(newData);

        counter++;
      } else {
        newComp = new Composite(composite, SWT.BORDER);
        newCompGridData.horizontalAlignment = SWT.FILL;
        newCompGridData.grabExcessHorizontalSpace = true;
        newCompGridData.horizontalSpan = 5;
        newCompGridData.verticalAlignment = SWT.FILL;
        newComp.setLayoutData(newCompGridData);

        GridData newData = new GridData();
        newData.heightHint = 100;
        composite.setLayoutData(newData);

        Color red = display.getSystemColor(SWT.COLOR_RED);
        newComp.setBackground(red);


        counter++;
      }
    } else {
      newComp.setVisible(false);

      GridData newData = new GridData();
      newData.heightHint = 40;
      composite.setLayoutData(newData);

      counter++;
    }
    composite.getParent().layout();
    composite.layout();
    newComp.layout();
  }

Before resize

After resize

question from:https://stackoverflow.com/questions/65923406/swt-why-is-parent-composite-resized-after-i-create-a-new-composite-in-parent

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The lines:

GridData newData = new GridData();
newData.heightHint = 100;
composite.setLayoutData(newData);

are changing the layout data for composite to the default alignment and grab values. They should be:

GridData newData = new GridData(SWT.FILL, SWT.FILL, true, false);
newData.heightHint = 100;
composite.setLayoutData(newData);

Note: the line

outerComp.setLayoutData(gridDataOuterComp);

doesn't do anything because the parent of outerComp is using FillLayout.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...