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:
- 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. - Create a GridItem that is the root of the tree. This GridItem should have the Grid as its parent.
- 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




