Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Expandbar in an Expandbar (Problems resizing / layouting an Expandbar.)
Expandbar in an Expandbar [message #780162] Tue, 17 January 2012 09:37 Go to next message
Jan Mielke is currently offline Jan MielkeFriend
Messages: 2
Registered: January 2012
Junior Member
Hi everyone and thanks for taking time looking at my problem.

I have a class that uses an Expandbar in order to show some values.
I need to be able to use this class again as a value, but I cant find
a way that the items of the second Expandbar are layoutet as I want.

In my example code:

How can I make "Item 1" being sized "the right way" (not being scrolled)
after it was expanded? (Resize the shell after "Item 1" was expanded
and you see what I mean.)





package Expandbar;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ExpandEvent;
import org.eclipse.swt.events.ExpandListener;
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.ExpandBar;
import org.eclipse.swt.widgets.ExpandItem;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;


public class MExpandbar extends Composite{
	
	// just to make out if I need to create a second MExpandbar in this example
	private final int type;
	private ExpandBar bar;
	protected final ExpandItem parentItem;
	
	public MExpandbar(Composite parent, int type, ExpandItem pItem) {
		
		super(parent, SWT.NONE);
		
		this.type = type;
		this.parentItem = pItem;
		
		this.setLayout(new FillLayout());
		
		this.bar = new ExpandBar(this, SWT.V_SCROLL);
		ExpandItem item0 = new ExpandItem (bar, SWT.NONE);
		
		// Creating the content that is set to the ExpandItem
		Composite composite = new Composite (bar, SWT.NONE);
		composite.setLayout(new GridLayout(2, false));
		
		for(int i = 0; i < 5; i++) {
			Label lbl = new Label(composite, SWT.NONE);
			lbl.setText("Label: "+i);
			
			Text txt = new Text(composite, SWT.BORDER);
			txt.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		}
		
		if(this.type == 0) {
			
			Label lbl = new Label(composite, SWT.NONE);
			lbl.setText("Exbar:");
			lbl.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING | GridData.VERTICAL_ALIGN_BEGINNING));
			
			Composite cmpNewBar = new Composite(composite, SWT.NONE);
			cmpNewBar.setLayout(new FillLayout());
			cmpNewBar.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
			new MExpandbar(cmpNewBar, 1, item0);
		}
	
		item0.setText("Item "+type);
		item0.setHeight(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
		item0.setControl(composite);
		
		bar.addExpandListener(new ExpandListener() {
			
			@Override
			public void itemExpanded(ExpandEvent arg0) {
				
				final ExpandItem item0 = (ExpandItem)arg0.item;
				
				if(parentItem != null) {
					parentItem.setHeight(parentItem.getHeight()+item0.getHeight());
				}
			}
			
			@Override
			public void itemCollapsed(ExpandEvent arg0) {
				
				ExpandItem item0 = (ExpandItem)arg0.item;
				
				if(parentItem != null) {
					parentItem.setHeight(parentItem.getHeight()-item0.getHeight());					
				}

			}
		});
	}
	
	
	public static void main(String[] args) {
		Display display = new Display ();
		Shell shell = new Shell (display);
		shell.setLayout(new FillLayout());
		shell.setText("ExpandBar Example");
		
		Composite cmpExpandbar = new Composite(shell, SWT.NONE);
		cmpExpandbar.setLayout(new FillLayout());
		
		new MExpandbar(cmpExpandbar, 0, null);
		
		shell.setSize(400, 350);
		shell.open();
		while (!shell.isDisposed ()) {
			if (!display.readAndDispatch ()) {
				display.sleep ();
			}
		}
		
		display.dispose();
	}
}


index.php/fa/6759/0/
  • Attachment: 00_exbar.jpg
    (Size: 22.09KB, Downloaded 636 times)

[Updated on: Tue, 17 January 2012 09:38]

Report message to a moderator

Re: Expandbar in an Expandbar [message #780229 is a reply to message #780162] Tue, 17 January 2012 12:10 Go to previous message
Jan Mielke is currently offline Jan MielkeFriend
Messages: 2
Registered: January 2012
Junior Member
After trying out nearly everything I found my mistake...
Of cause the GridLayoutData of the Composite on which the second ExpandBar ist placed must be GridData.FillBoth...

The following "debug-code" works for me:

package Expandbar;

import java.util.TreeMap;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ExpandEvent;
import org.eclipse.swt.events.ExpandListener;
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.ExpandBar;
import org.eclipse.swt.widgets.ExpandItem;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;


public class MExpandbar extends Composite{
	
	private final static TreeMap<String, Composite> tmComposites = new TreeMap<String, Composite>();
	
	private final static String KEY_COMP_SHELL = "CompShell";
	private final static String KEY_COMP_MEX0 = "CompMex0";
	private final static String KEY_COMP_MEX1 = "CompMex1";
	private final static String KEY_COMP_Cont0 = "CompCont0";
	private final static String KEY_COMP_Cont1 = "CompCont1";
	private final static String KEY_COMP_GRID4MEX1 = "Comp4Mex1";
	
	
	// just to make out if I need to create a second MExpandbar in this example
	private final int type;
	private ExpandBar bar;
	protected final ExpandItem parentItem;
	
	public MExpandbar(Composite parent, int type, ExpandItem pItem) {
		
		super(parent, SWT.NONE);
		
		this.type = type;
		this.parentItem = pItem;
		
		this.setLayout(new FillLayout());
		
		if(this.type == 0) {
			this.bar = new ExpandBar(this, SWT.V_SCROLL);
		}
		else {
			this.bar = new ExpandBar(this, SWT.NONE);
		}
		ExpandItem item0 = new ExpandItem (bar, SWT.NONE);
		
		// Creating the content that is set to the ExpandItem
		Composite composite = new Composite (bar, SWT.NONE);
		composite.setLayout(new GridLayout(2, false));
		
		for(int i = 0; i < 5; i++) {
			Label lbl = new Label(composite, SWT.NONE);
			lbl.setText("Label: "+i);
			
			Text txt = new Text(composite, SWT.BORDER);
			txt.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		}
		
		if(this.type == 0) {
			
			MExpandbar.tmComposites.put(MExpandbar.KEY_COMP_MEX0, this);
			MExpandbar.tmComposites.put(MExpandbar.KEY_COMP_Cont0, composite);
			
			Label lbl = new Label(composite, SWT.NONE);
			lbl.setText("Exbar:");
			lbl.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING | GridData.VERTICAL_ALIGN_BEGINNING));
			
			Composite cmpNewBar = new Composite(composite, SWT.NONE);
			cmpNewBar.setLayout(new FillLayout());
			cmpNewBar.setLayoutData(new GridData(GridData.FILL_BOTH));
			
			MExpandbar.tmComposites.put(MExpandbar.KEY_COMP_GRID4MEX1, composite);
			new MExpandbar(cmpNewBar, 1, item0);
		}
		else {
			MExpandbar.tmComposites.put(MExpandbar.KEY_COMP_MEX1, this);
			MExpandbar.tmComposites.put(MExpandbar.KEY_COMP_Cont1, composite);
		}
	
		item0.setText("Item "+type);
		item0.setHeight(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
		item0.setControl(composite);
		
		bar.addExpandListener(new ExpandListener() {
			
			@Override
			public void itemExpanded(ExpandEvent arg0) {
				
				final ExpandItem item0 = (ExpandItem)arg0.item;
				
				if(parentItem != null) {
					parentItem.setHeight(parentItem.getHeight()+item0.getHeight());
				}
				
				// Wenn dieser code ausgeführt wird, wird nach schließen und öffnen des ersten Items alles richtig angezeigt...
				MExpandbar.tmComposites.get(MExpandbar.KEY_COMP_SHELL).layout(true, true);
			}
			
			@Override
			public void itemCollapsed(ExpandEvent arg0) {
				
				ExpandItem item0 = (ExpandItem)arg0.item;
				
				if(parentItem != null) {
					parentItem.setHeight(parentItem.getHeight()-item0.getHeight());					
				}

			}
		});
	}
	
	
	public static void main(String[] args) {
		Display display = new Display ();
		Shell shell = new Shell (display);
		shell.setLayout(new FillLayout());
		shell.setText("ExpandBar Example");
		
		Composite cmpExpandbar = new Composite(shell, SWT.NONE);
		cmpExpandbar.setData("Shell");
		MExpandbar.tmComposites.put(MExpandbar.KEY_COMP_SHELL, cmpExpandbar);
		cmpExpandbar.setLayout(new FillLayout());
		
		new MExpandbar(cmpExpandbar, 0, null);
		
		shell.setSize(400, 350);
		shell.open();
		while (!shell.isDisposed ()) {
			if (!display.readAndDispatch ()) {
				display.sleep ();
			}
		}
		
		display.dispose();
	}
}

Previous Topic:log4j appender for SWT Text
Next Topic:libswt-webkit-gtk is no longer built in Eclipse 3.8
Goto Forum:
  


Current Time: Sat Nov 29 07:51:35 GMT 2014

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

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