Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Tree resizes not correctly after expanding/collapsing
Tree resizes not correctly after expanding/collapsing [message #629747] Wed, 29 September 2010 13:06 Go to next message
Marcel M. is currently offline 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

Re: Tree resizes not correctly after expanding/collapsing [message #630060 is a reply to message #629747] Thu, 30 September 2010 14:26 Go to previous messageGo to next message
Lakshmi Shanmugam
Messages: 274
Registered: July 2009
Location: India
Senior Member
Hi,

The Tree's new size (as a result of expand/collapse) is not yet set when the Expand/Collapse event is sent. You can use asynExec() to allow the Tree's size to be set before composite.layout() is called.

public void treeExpanded(TreeEvent e) {
display.asyncExec(new Runnable() {
public void run() {
composite.layout();
}
});
}


Lakshmi P Shanmugam
Re: Tree resizes not correctly after expanding/collapsing [message #630079 is a reply to message #630060] Thu, 30 September 2010 15:12 Go to previous message
Marcel M. is currently offline Marcel M.
Messages: 2
Registered: September 2010
Junior Member
Hi,

that works.

Thanks,
Marcel
Previous Topic:OLEAutomation - Disable Existing IE Toolbar
Next Topic:Windows CE java.lang.UnsatisfiedLinkError org/eclipse/swt/internal/win32/OS.SetProcessDPIAware()Z
Goto Forum:
  


Current Time: Thu Oct 23 03:14:19 GMT 2014

Powered by FUDForum. Page generated in 0.01678 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software