public class GridExample2 {
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);
grid.setRowHeaderVisible(true); // show Row Headers
grid.setCellSelectionEnabled(true); //allow Cell Selection
Car car1 = new Car(133, "2007","Honda",
"CR-V",Car.CarType.SUV, 322, "Glacier Blue", true);
Car car2 = new Car(134, "2002","BMW",
"M Roadster",Car.CarType.CONVERTIBLE, 40233, "Red", false);
|-------10--------20--------30--------40--------50--------60--------70--------80--------9|
|-------- XML error: The previous line is longer than the max of 90 characters ---------|
Car car3 = new Car(135, "2002","Acura",
"RSX",Car.CarType.COUPE, 53283, "Black", false);
GridColumn idColumn = new GridColumn(grid, SWT.NONE);
idColumn.setText("Car Number");
idColumn.setWidth(100);
//don't allow cells in the idColumn to be selected
idColumn.setCellSelectionEnabled(false);
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);
availableColumn.setText("Available");
availableColumn.setWidth(75);
GridItem item1 = new GridItem(grid, SWT.NONE);
item1.setHeaderText("Row Header");
item1.setText(0, String.valueOf(car1.getCarNumber()));
item1.setText(1,car1.getYear());
item1.setText(2, car1.getMake());
item1.setText(3, car1.getModel());
item1.setText(4, car1.getCarType().toString());
//set whether the check box in this column and row is checked
item1.setChecked(5, car1.isAvailable());
GridItem item2 = new GridItem(grid, SWT.NONE);
item2.setText(0, String.valueOf(car2.getCarNumber()));
item2.setText(1,car2.getYear());
item2.setText(2, car2.getMake());
item2.setText(3, car2.getModel());
item2.setText(4, car2.getCarType().toString());
item2.setChecked(5, car2.isAvailable());
//set background to blue
item2.setBackground(4,new Color(null, 0,0,255));
//change font
item2.setFont(4, new Font(null, "Arial", 12, SWT.BOLD | SWT.ITALIC));
//set text color to red
item2.setForeground(4, new Color(null, 255, 0, 0));
GridItem item3 = new GridItem(grid, SWT.NONE);
item3.setText(0, String.valueOf(car3.getCarNumber()));
item3.setText(1, car3.getYear());
item3.setText(2, car3.getMake());
item3.setText(3, car3.getModel());
item3.setText(4, car3.getCarType().toString());
item3.setChecked(5, car2.isAvailable());
shell.setSize(625, 200);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
} |