I’m creating an application in Java and SWT and I have a problem. I want to put ExpandBar into GridLayout. Everything is ok while I don’t collapse the expand bar. When I do it, it collpases normally, but the composite isn’t re-layouted. The screen below should explain my problem:
Before collapse:

After collapse:

I tried some tricks, like for example:
bar.addExpandListener(new ExpandListener() {
public void itemExpanded(ExpandEvent e) {
item0.setHeight(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
gridData2.heightHint = composite.computeSize(SWT.DEFAULT,
SWT.DEFAULT).y;
comp.layout(true, true);
}
public void itemCollapsed(ExpandEvent e) {
item0.setHeight(item0.getHeaderHeight());
gridData2.heightHint = item0.getHeaderHeight();
comp.layout(true, true);
}
});
Altough it doesn’t work. Here is SSCCE:
package stackoverflow;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.ExpandBar;
import org.eclipse.swt.widgets.ExpandItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
public class MainWindowSWT extends Window {
public MainWindowSWT() {
super((Shell)null);
setBlockOnOpen(true);
open();
Display.getCurrent().dispose();
}
@Override
protected Control createContents(Composite parent) {
final Composite comp = new Composite(getShell(), 0);
GridLayout layout = new GridLayout(1, true);
comp.setLayout(layout);
Tree tree = new Tree(comp, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
GridData gridData = new GridData();
gridData.verticalAlignment = GridData.FILL;
gridData.verticalSpan = 2;
gridData.grabExcessVerticalSpace = true;
gridData.horizontalAlignment = GridData.FILL;
gridData.grabExcessHorizontalSpace = true;
tree.setLayoutData(gridData);
final ExpandBar bar = new ExpandBar(comp, SWT.V_SCROLL);
final Composite composite = new Composite(bar, SWT.NONE);
GridLayout gridLayout = new GridLayout();
gridLayout.verticalSpacing = 10;
composite.setLayout(gridLayout);
Button button = new Button(composite, SWT.PUSH);
button.setText("SWT.PUSH");
final ExpandItem item0 = new ExpandItem(bar, SWT.NONE, 0);
item0.setText("Test");
item0.setHeight(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
item0.setControl(composite);
item0.setExpanded(true);
final GridData gridData2 = new GridData();
gridData2.horizontalAlignment = GridData.FILL;
gridData2.grabExcessHorizontalSpace = true;
bar.setLayoutData(gridData2);
return parent;
}
}
Ubuntu 12.04.1 amd64, SWT 3.7.2.
Thanks in advance.
// Btw: I don’t want to resize shell. I want to resize the tree above the expand bar.
I get it working, but I’m not satisfied with this solution…