Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your profile (name, country/region, and company) is displayed to the public and will accompany any content you post. You may update your IBM account at any time.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

An Eclipse Nebula widgets primer

A quick guide to Eclipse Nebula's Grid, CDateTime, CompositeTable, PGroup and PShelf widgets

Scott Delap (scott@clientjava.com), President, Rich Client Solutions Inc.
Scott Delap is president of Rich Client Solutions Inc., a software consulting firm focusing on technologies such as Swing, Eclipse RCP, GWT, Flex, and Open Laszlo. He is actively involved in the Java community, speaking at events such as NFJS, QCon and JavaOne. He is also the Java editor of InfoQ.com and runs ClientJava.com, a portal focused on desktop Java development.
Barry Livingston (iblivin@gmail.com), Senior Software Engineer, MichaelDKelly.com
Barry Livingston has been developing software for more than eight years. A specialist in rich client development, he is focused on client technologies such as Swing and Eclipse RCP. Active in the Java community, he has often partnered with Rich Client Solutions to provide high-quality rich client development and consulting services to a variety of clients.

Summary:  The SWT toolkit offers a robust interface to the native widgets of the operating system it's running on. However, native widgets often aren't enough. The Eclipse Nebula project is working to bridge this gap with custom widgets for functionality, including calendaring and advanced tables. This tutorial demonstrates five Nebula widgets, including Grid, CDateTime, CompositeTable, PGroup, and PShelf.

Date:  17 Apr 2007
Level:  Intermediate PDF:  A4 and Letter (1206 KB | 47 pages)Get Adobe® Reader®

Activity:  28837 views
Comments:  

Create trees of Grid items and column spans

The Grid lets you create trees of GridItems, signifying a relationship between items and subitems. Items can be nested n layers deep, although simple one- or two-layer nestings are much more common. To create a tree of items:

  1. On the appropriate GridColumn, call the setTree() method, passing true as the value. This designates the column as a column that allows a tree of subitems. The UI control for toggling the view of branch objects appears in this column.
  2. Create a GridItem that is the root of the tree. This GridItem should have the Grid as its parent.
  3. Create GridItems that are the next branch of the tree. To do so, pass the reference to the parent object (in this case, the root of the tree) to the constructor of each branch GridItem. Repeat this step for each level of the tree. The parent object passed to the constructor of each item is the GridItem object one level above it in the tree.

A data field in a GridItem can also be set to span a number of columns. To do so, call the setColumnSpan(int,int) method of the appropriate GridItem. The first argument is the index of the column that will be affected; the second argument specifies the number of subsequent columns that will be spanned.

In Listing 7, trees of GridItems are created to group cars into a specific rental category. The category column is set to span across all columns. Its background color and font are also changed.


Listing 7. GridExample3
                    
public class GridExample3 {
	public static void main(String... args) {
		Display display = new Display();
		Shell shell = new Shell(display);
		shell.setLayout(new FillLayout());

	           Grid grid = new Grid(shell, SWT.BORDER | 
				SWT.V_SCROLL | SWT.H_SCROLL);
			   grid.setHeaderVisible(true);

	           		  Car car1 = new Car(133, "2007","Chevy", 
						"Cobalt",Car.CarType.COUPE, \
4321, "Yellow", true);
                      Car car2 = new Car(134, "2007","Chevy", 
						"Cobalt",Car.CarType.COUPE, \
4321, "Yellow", true);
                      Car car3 = new Car(135, "2006","Ford", 
						"Focus",Car.CarType.COUPE, \
15343, "Red", true);
                      Car car4 = new Car(136, "2006","Chrysler", 
						"Sebring",Car.CarType.SEDAN, \
12932, "Black", false);
                      Car car5 = new Car(137, "2002","Ford", 
						"Mustang",Car.CarType.COUPE, \
4342, "Red", true);

                      GridColumn rentalTypeColumn = new GridColumn(grid, SWT.NONE);
		rentalTypeColumn.setText("Rental Grade");
		rentalTypeColumn.setWidth(100);
		rentalTypeColumn.setTree(true);
		
		GridColumn idColumn = new GridColumn(grid, SWT.NONE);
		idColumn.setText("Car Number");
		idColumn.setWidth(100);
				
		GridColumn yearColumn = new GridColumn(grid, SWT.NONE);
		yearColumn.setText("Year");
		yearColumn.setWidth(50);

		GridColumn makeColumn = new GridColumn(grid, SWT.NONE);
		makeColumn.setText("Make");
		makeColumn.setWidth(100);

                      GridColumn modelColumn = new GridColumn(grid, SWT.NONE);
		modelColumn.setText("Model");
		modelColumn.setWidth(100);

		GridColumn typeColumn = new GridColumn(grid, SWT.NONE);
		typeColumn.setText("Type");
		typeColumn.setWidth(100);
		
	           GridColumn availableColumn = new GridColumn(grid, 
				SWT.CHECK | SWT.CENTER);
		availableColumn.setText("Available");
		availableColumn.setWidth(75);
		
		GridItem compactItem = new GridItem(grid, SWT.CENTER);
		compactItem.setText(0, "Compact");

	           compactItem.setFont(new Font(null, 
				"Arial", 18, SWT.BOLD | SWT.ITALIC));
		compactItem.setColumnSpan(0, 6);
		compactItem.setBackground(0, new Color(null, 0,255,0));
		
		GridItem item1 = new GridItem(compactItem, SWT.NONE);
		item1.setText(1, String.valueOf(car1.getCarNumber()));
		item1.setText(2,car1.getYear());
		item1.setText(3, car1.getMake());
		item1.setText(4, car1.getModel());
		item1.setText(5, car1.getCarType().toString());
		item1.setChecked(6, car1.isAvailable());
		

		GridItem item2 = new GridItem(compactItem, SWT.NONE);
		item2.setText(1, String.valueOf(car2.getCarNumber()));
		item2.setText(2,car2.getYear());
		item2.setText(3, car2.getMake());
		item2.setText(4, car2.getModel());
		item2.setText(5, car2.getCarType().toString());
		item2.setChecked(6, car2.isAvailable());
		
		GridItem item3 = new GridItem(compactItem, SWT.NONE);
		item3.setText(1, String.valueOf(car3.getCarNumber()));
		item3.setText(2, car3.getYear());
		item3.setText(3, car3.getMake());
		item3.setText(4, car3.getModel());
		item3.setText(5, car3.getCarType().toString());
		item3.setChecked(6, car3.isAvailable());
		
		GridItem midSizedItem = new GridItem(grid, SWT.NONE);
		midSizedItem.setText(0, "Mid-Sized");

                      midSizedItem.setFont(new Font(null, 
						"Arial", 18, SWT.BOLD | SWT.ITALIC));
		midSizedItem.setColumnSpan(0,6);
		midSizedItem.setBackground(0, new Color(null, 0, 255, 255));
		
		GridItem item4 = new GridItem(midSizedItem, SWT.NONE);
		item4.setText(1, String.valueOf(car4.getCarNumber()));
		item4.setText(2,car4.getYear());
		item4.setText(3, car4.getMake());
		item4.setText(4, car4.getModel());
		item4.setText(5, car4.getCarType().toString());
		item4.setChecked(6, car4.isAvailable());
		
		GridItem item5 = new GridItem(midSizedItem, SWT.NONE);
		item5.setText(1, String.valueOf(car5.getCarNumber()));
		item5.setText(2,car5.getYear());
		item5.setText(3, car5.getMake());
		item5.setText(4, car5.getModel());
		item5.setText(5, car5.getCarType().toString());
		item5.setChecked(6, car5.isAvailable());
		
		shell.setSize(700, 200);
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}

}

Running the example provides the result shown below.


Figure 18. GridExample3
GridExample3

14 of 20 | Previous | Next

Comments



static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Open source
ArticleID=206190
TutorialTitle=An Eclipse Nebula widgets primer
publish-date=04172007
author1-email=scott@clientjava.com
author1-email-cc=
author2-email=iblivin@gmail.com
author2-email-cc=