SWT – Group Example

What is Group

In SWT, Group is a subclass of a Composite class. Group is used to improve application appearance and make whole application look more organized. It will draw a rectangular border around all it’s child widgets.

Group widget support five styles (actually it is nothing much different)

1) SWT.SHADOW_IN
2) SWT.SHADOW_OUT
3) SWT.SHADOW_NONE
4) SWT.SHADOW_ETCHED_IN
5) SWT.SHADOW_ETCHED_OUT

How to create a Group widget?

To make it more organize , Group is usually created in separate class and it have to extend the Composite class.

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;

public class SWTGroup extends Composite
{
	public SWTGroup(Composite parent)
	{
		super(parent, SWT.NONE);
		this.setSize(300, 300);
		
		Group group = new Group(this, SWT.SHADOW_ETCHED_IN);
		group.setLocation(50, 50);
		
		group.setText("Group SHADOW_IN");
		
		Label label = new Label(group, SWT.NONE);
		label.setText("Label in Group");
		label.setLocation(20,20);
		label.pack();
		
		Button button = new Button(group, SWT.PUSH);
		button.setText("Push button in Group");
		button.setLocation(20,45);
		button.pack();
		
		group.pack();
		
	}
}

The SWTGroup (Group class) can’t be directly executed, it needs an application to invoke it’s constructor.

Here i create a Main class to call SWTGroup and add it to Shell for display.

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
 
public class SWTMain {
 
public static void main (String [] args) {
	Display display = new Display ();
	Shell shell = new Shell(display);
	shell.setText("SWT Group Example");
	
	SWTGroup swtGroup = new SWTGroup(shell);
	
	shell.pack();
	shell.open();
	
	while (!shell.isDisposed ()) {
		if (!display.readAndDispatch ()) display.sleep ();
	}
	display.dispose ();
}
}

Why i need create a separate Group class?

It’s just nothing wrong to include Group class in Main Shell display class. Separate Group class will make our SWT application more organized and easy to maintain.

Here is the example to include all together in one class, it’s just a bit messy…

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
 
public class SWTMain {
 
public static void main (String [] args) {
	Display display = new Display ();
	Shell shell = new Shell(display);
	shell.setText("SWT Group Example");
	
	Group group = new Group(shell, SWT.SHADOW_IN);
	group.setLocation(50, 50);
	
	group.setText("Group SHADOW_IN");
	
	Label label = new Label(group, SWT.NONE);
	label.setText("Label in Group");
	label.setLocation(20,20);
	label.pack();
	
	Button button = new Button(group, SWT.PUSH);
	button.setText("Push button in Group");
	button.setLocation(20,45);
	button.pack();
	
	group.pack();
	
	shell.setSize(500,500);
	shell.open();
	
	while (!shell.isDisposed ()) {
		if (!display.readAndDispatch ()) display.sleep ();
	}
	display.dispose ();
}
}

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
2 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
2 Comment authors
zengByfou Recent comment authors
newest oldest most voted
zeng
Guest
zeng

It broke my heart to see that the last record was nine years ago

Byfou
Guest
Byfou

Good example but I find SWT quite complex…
There are lot of event to manage, is there any over class that could manage that more easily and where you should indicate only what you want to do ?