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();
}
question from:
https://stackoverflow.com/questions/65923406/swt-why-is-parent-composite-resized-after-i-create-a-new-composite-in-parent 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…