Tree resizes not correctly after expanding/collapsing [message #629747] |
Wed, 29 September 2010 13:06 |
Marcel M. Messages: 2 Registered: September 2010 |
Junior Member |
|
|
Hi,
I have some vertical aligned tree widgets, which are childs of a composite with grid layout. In the "normal" implementation an respective tree widget don't resize after expanding a tree item, but shows scroll bars. Because I want see the full tree, I listen to the tree events and call the layout method of the parent after an expand or collapse event. But with this implementation I get a strange effect: After expanding the widget gets the size of the collapsed tree and after collapsing the widget gets the size of the expanded tree. What is wrong with my implementation?
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.TreeEvent;
import org.eclipse.swt.events.TreeListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
public class Snippet8 implements TreeListener {
private Composite composite;
public static void main(String[] args) {
new Snippet8();
}
public Snippet8() {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("Lazy Tree");
shell.setLayout(new FillLayout());
ScrolledComposite control = new ScrolledComposite(shell, SWT.H_SCROLL
| SWT.V_SCROLL);
control
.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1,
1));
control.setExpandHorizontal(true);
control.setExpandVertical(true);
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 1;
GridData gridData = new GridData();
gridData.horizontalAlignment = GridData.FILL;
gridData.grabExcessHorizontalSpace = true;
gridData.grabExcessVerticalSpace = true;
gridData.verticalAlignment = GridData.CENTER;
composite = new Composite(control, SWT.NONE);
composite.setLayoutData(gridData);
composite.setLayout(gridLayout);
//dummy-tree a
Tree treeA = new Tree(composite, SWT.BORDER);
treeA.addTreeListener(this);
for (int i = 1; i < 5; i++) {
TreeItem root = new TreeItem(treeA, 0);
root.setText("A" + String.valueOf(i));
for (int j = 1; j < 5; j++) {
TreeItem child = new TreeItem(root, 0);
child.setText("A" + String.valueOf(i) + String.valueOf(j));
}
}
//dummy-tree b
Tree treeB = new Tree(composite, SWT.BORDER);
for (int i = 1; i < 5; i++) {
TreeItem root = new TreeItem(treeB, 0);
root.setText("B" + String.valueOf(i));
for (int j = 1; j < 5; j++) {
TreeItem child = new TreeItem(root, 0);
child.setText("B" + String.valueOf(i) + String.valueOf(j));
}
}
control.setMinSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
control.setContent(composite);
shell.setSize(500, 500);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
@Override
public void treeCollapsed(TreeEvent e) {
composite.layout();
}
@Override
public void treeExpanded(TreeEvent e) {
composite.layout();
}
}
Thanks in advance,
Marcel
[Updated on: Wed, 29 September 2010 13:08] Report message to a moderator
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.04949 seconds