|
|
|
第 15 页,共 21 页 |
|
|
|
列组 Nebula Grid 使您可以把相关列分组到 GridColumnGroup 中。这是通过创建 GridColumnGroup 对象,指定 Grid 作为其在构造函数中的父对象和 SWT.NONE 的样式来完成的。然后将在组中创建列,把对新创建的 GridColumnGroup 的引用作为父对象传递给每个 GridColumn 的构造函数。 清单 8 中的示例是在先前示例基础上创建两个列组构建的:一个用于基本的汽车信息,一个用于汽车的详细信息。还添加了一些详细信息字段。 清单 8. GridExample4
public class GridExample4 {
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);
GridColumnGroup carGroup = new GridColumnGroup(grid, SWT.NONE);
carGroup.setText("Automobile");
GridColumn yearColumn = new GridColumn(carGroup, SWT.NONE);
yearColumn.setText("Year");
yearColumn.setWidth(50);
GridColumn makeColumn = new GridColumn(carGroup, SWT.NONE);
makeColumn.setText("Make");
makeColumn.setWidth(100);
GridColumn modelColumn = new GridColumn(carGroup, SWT.NONE);
modelColumn.setText("Model");
modelColumn.setWidth(100);
GridColumnGroup carDetailsGroup =
new GridColumnGroup(grid, SWT.NONE);
carDetailsGroup.setText("Car Details");
GridColumn idColumn = new GridColumn(carDetailsGroup, SWT.NONE);
idColumn.setText("Car Number");
idColumn.setWidth(100);
GridColumn typeColumn = new GridColumn(carDetailsGroup, SWT.NONE);
typeColumn.setText("Type");
typeColumn.setWidth(100);
GridColumn mileageColumn =
new GridColumn(carDetailsGroup, SWT.NONE);
mileageColumn.setText("Mileage");
mileageColumn.setWidth(100);
GridColumn colorColumn = new GridColumn(carDetailsGroup, SWT.NONE);
colorColumn.setText("Color");
colorColumn.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", 16, SWT.ITALIC));
compactItem.setColumnSpan(0, 8);
compactItem.setBackground(0, new Color(null, 0,255,0));
addCarRow(car1, compactItem);
addCarRow(car2, compactItem);
addCarRow(car3, compactItem);
GridItem midSizedItem = new GridItem(grid, SWT.NONE);
midSizedItem.setText(0, "Mid-Sized");
midSizedItem.setFont(new Font(null, "Arial", 16, SWT.ITALIC));
midSizedItem.setColumnSpan(0,8);
midSizedItem.setBackground(0, new Color(null, 0, 255, 255));
addCarRow(car4, midSizedItem);
addCarRow(car5, midSizedItem);
shell.setSize(850, 200);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
private static void addCarRow(Car car, GridItem parentItem) {
GridItem item1 = new GridItem(parentItem, SWT.NONE);
item1.setText(1,car.getYear());
item1.setText(2, car.getMake());
item1.setText(3, car.getModel());
item1.setText(4, String.valueOf(car.getCarNumber()));
item1.setText(5, car.getCarType().toString());
item1.setText(6, String.valueOf(car.getMileage()));
item1.setText(7, car.getColor());
item1.setChecked(8, car.isAvailable());
}
} |
运行示例将提供如下所示的结果。 图 19. GridExample4
创建可展开的列组 除了分组相关列以外,您可以以允许展开和收缩、隐藏一些列的方式来创建 GridColumnGroup。为此,在构造 GridColumnGroup 时需要传递 SWT.TOGGLE 的样式。可以通过调用 setExpanded() 方法来设定 GridColumnGroup 最初是展开状态还是收缩状态。 您必须指定属于组的每个 GridColumn 是摘要字段,还是详细信息字段(或两者皆是)。这是通过分别调用 GridColumn 的 setDetail() 和 setSummary() 方法来完成的。默认情况下,假定任何未显式设为详细信息字段的列既是摘要列也是详细信息列,并以展开和收缩状态的 GridColumnGroup 显示。 清单 9 中的示例将创建一个激活的 GridColumnGroup,它将显示每辆汽车的详细信息。该组最初是处于收缩状态。 清单 9. GridExample5
public class GridExample5 {
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);
|-------10--------20--------30--------40--------50--------60--------70--------80--------9|
|-------- XML error: The previous line is longer than the max of 90 characters ---------|
Car car2 = new Car(134, "2007","Chevy",
"Cobalt",Car.CarType.COUPE, 6435, "Yellow", true);
|-------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, "2006","Ford",
"Focus",Car.CarType.COUPE, 15343, "Red", true);
|-------10--------20--------30--------40--------50--------60--------70--------80--------9|
|-------- XML error: The previous line is longer than the max of 90 characters ---------|
Car car4 = new Car(136, "2006","Chrysler",
"Sebring",Car.CarType.SEDAN, 12932, "Black", false);
|-------10--------20--------30--------40--------50--------60--------70--------80--------9|
|-------- XML error: The previous line is longer than the max of 90 characters ---------|
Car car5 = new Car(137, "2002","Ford",
"Mustang",Car.CarType.COUPE,4342, "Red", true);
|-------10--------20--------30--------40--------50--------60--------70--------80--------9|
|-------- XML error: The previous line is longer than the max of 90 characters ---------|
GridColumn rentalTypeColumn = new GridColumn(grid, SWT.NONE);
rentalTypeColumn.setText("Rental Grade");
rentalTypeColumn.setWidth(100);
rentalTypeColumn.setTree(true);
GridColumnGroup carGroup = new GridColumnGroup(grid, SWT.NONE);
carGroup.setText("Automobile");
GridColumn yearColumn = new GridColumn(carGroup, SWT.NONE);
yearColumn.setText("Year");
yearColumn.setWidth(50);
GridColumn makeColumn = new GridColumn(carGroup, SWT.NONE);
makeColumn.setText("Make");
makeColumn.setWidth(100);
GridColumn modelColumn = new GridColumn(carGroup, SWT.NONE);
modelColumn.setText("Model");
modelColumn.setWidth(100);
GridColumnGroup carDetailsGroup =
new GridColumnGroup(grid, SWT.TOGGLE);
carDetailsGroup.setText("Car Details");
// set the group to start off contracted
carDetailsGroup.setExpanded(false);
GridColumn idColumn = new GridColumn(carDetailsGroup, SWT.NONE);
idColumn.setText("Car Number");
idColumn.setWidth(100);
GridColumn typeColumn = new GridColumn(carDetailsGroup, SWT.NONE);
typeColumn.setText("Type");
typeColumn.setWidth(100);
// all of other the columns in the group are detail fields
typeColumn.setDetail(true);
typeColumn.setSummary(false);
GridColumn mileageColumn =
new GridColumn(carDetailsGroup, SWT.NONE);
mileageColumn.setText("Mileage");
mileageColumn.setWidth(100);
mileageColumn.setDetail(true);
mileageColumn.setSummary(false);
GridColumn colorColumn = new GridColumn(carDetailsGroup, SWT.NONE);
colorColumn.setText("Color");
colorColumn.setWidth(100);
colorColumn.setDetail(true);
colorColumn.setSummary(false);
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", 16, SWT.ITALIC));
compactItem.setColumnSpan(0, 8);
compactItem.setBackground(0, new Color(null, 0,255,0));
addCarRow(car1, compactItem);
addCarRow(car2, compactItem);
addCarRow(car3, compactItem);
GridItem midSizedItem = new GridItem(grid, SWT.NONE);
midSizedItem.setText(0, "Mid-Sized");
midSizedItem.setFont(new Font(null, "Arial", 16, SWT.ITALIC));
midSizedItem.setColumnSpan(0,8);
midSizedItem.setBackground(0, new Color(null, 0, 255, 255));
addCarRow(car4, midSizedItem);
addCarRow(car5, midSizedItem);
shell.setSize(850, 200);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
private static void addCarRow(Car car, GridItem parentItem) {
GridItem item1 = new GridItem(parentItem, SWT.NONE);
item1.setText(1,car.getYear());
item1.setText(2, car.getMake());
item1.setText(3, car.getModel());
item1.setText(4, String.valueOf(car.getCarNumber()));
item1.setText(5, car.getCarType().toString());
item1.setText(6, String.valueOf(car.getMileage()));
item1.setText(7, car.getColor());
item1.setChecked(8, car.isAvailable());
}
} |
运行示例将提供如下所示的结果(这里所示的是其处于展开状态)。 图 20. GridExample5
添加一个摘要列 如前述,您可以指定 GridColumnGroup 中一个或多个 GridColumn 作为摘要字段。此字段对于显示摘要值和允许用户交替打开详细信息非常有用。清单 10 将创建另一个 GridColumnGroup,它将在收缩时显示日租金的总价格。展开时,将显示这个总价的详细信息以及 Total Price 列。注意:Total Price 列是怎样被指定为摘要列和详细信息列。如果发送 false 作为此 GridColumn 上的 setDetail() 方法的参数,则在展开组时 Total Price 列将消失。 清单 10. GridExample6
public class GridExample6 {
public static void main(String... args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
//defaults to SWT.SINGLE - other options MULTI, NO_FOCUS, CHECK
Grid grid = new Grid(shell, SWT.SINGLE |
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, 122.00, .075);
|-------10--------20--------30--------40--------50--------60--------70--------80--------9|
|-------- XML error: The previous line is longer than the max of 90 characters ---------|
Car car2 = new Car(134, "2007","Chevy",
"Cobalt",Car.CarType.COUPE, 6435, "Yellow", true, 122.00, .075);
|-------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, "2006","Ford",
"Focus",Car.CarType.COUPE, 15343, "Red", true, 122.00, .075);
|-------10--------20--------30--------40--------50--------60--------70--------80--------9|
|-------- XML error: The previous line is longer than the max of 90 characters ---------|
Car car4 = new Car(136, "2006","Chrysler",
"Sebring",Car.CarType.SEDAN, 12932, "Black", false, 144.00, .075);
|-------10--------20--------30--------40--------50--------60--------70--------80--------9|
|-------- XML error: The previous line is longer than the max of 90 characters ---------|
Car car5 = new Car(137, "2002","Ford",
"Mustang",Car.CarType.COUPE,4342, "Red", true, 144.00, .075);
|-------10--------20--------30--------40--------50--------60--------70--------80--------9|
|-------- XML error: The previous line is longer than the max of 90 characters ---------|
GridColumn rentalTypeColumn = new GridColumn(grid, SWT.NONE);
rentalTypeColumn.setText("Rental Grade");
rentalTypeColumn.setWidth(100);
rentalTypeColumn.setTree(true);
GridColumnGroup carGroup = new GridColumnGroup(grid, SWT.NONE);
carGroup.setText("Automobile");
GridColumn yearColumn = new GridColumn(carGroup, SWT.NONE);
yearColumn.setText("Year");
yearColumn.setWidth(50);
GridColumn makeColumn = new GridColumn(carGroup, SWT.NONE);
makeColumn.setText("Make");
makeColumn.setWidth(100);
GridColumn modelColumn = new GridColumn(carGroup, SWT.NONE);
modelColumn.setText("Model");
modelColumn.setWidth(100);
GridColumnGroup carDetailsGroup =
new GridColumnGroup(grid, SWT.TOGGLE);
carDetailsGroup.setText("Car Details");
carDetailsGroup.setExpanded(false);
GridColumn idColumn = new GridColumn(carDetailsGroup, SWT.NONE);
idColumn.setText("Car Number");
idColumn.setWidth(100);
GridColumn typeColumn = new GridColumn(carDetailsGroup, SWT.NONE);
typeColumn.setText("Type");
typeColumn.setWidth(100);
typeColumn.setDetail(true);
typeColumn.setSummary(false);
GridColumn mileageColumn =
new GridColumn(carDetailsGroup, SWT.NONE);
mileageColumn.setText("Mileage");
mileageColumn.setWidth(100);
mileageColumn.setDetail(true);
mileageColumn.setSummary(false);
GridColumn colorColumn = new GridColumn(carDetailsGroup, SWT.NONE);
colorColumn.setText("Color");
colorColumn.setWidth(100);
colorColumn.setDetail(true);
colorColumn.setSummary(false);
GridColumnGroup pricingGroup =
new GridColumnGroup(grid, SWT.TOGGLE);
pricingGroup.setText("Pricing");
pricingGroup.setExpanded(false);
GridColumn dailyRentalColumn =
new GridColumn(pricingGroup, SWT.NONE);
dailyRentalColumn.setText("Daily Rental");
dailyRentalColumn.setWidth(100);
dailyRentalColumn.setDetail(true);
dailyRentalColumn.setSummary(false);
GridColumn taxColumn = new GridColumn(pricingGroup, SWT.NONE);
taxColumn.setText("Tax");
taxColumn.setWidth(100);
taxColumn.setDetail(true);
taxColumn.setSummary(false);
GridColumn totalPriceColumn =
new GridColumn(pricingGroup, SWT.NONE);
totalPriceColumn.setText("Total");
totalPriceColumn.setWidth(100);
totalPriceColumn.setDetail(true);
totalPriceColumn.setSummary(true);
GridColumn availableColumn =
new GridColumn(grid, SWT.CHECK | SWT.CENTER);
|-------10--------20--------30--------40--------50--------60--------70--------80--------9|
|-------- XML error: The previous line is longer than the max of 90 characters ---------|
availableColumn.setText("Available");
availableColumn.setWidth(75);
GridItem compactItem = new GridItem(grid, SWT.CENTER);
compactItem.setText(0, "Compact");
compactItem.setFont(new Font(null, "Arial", 16, SWT.ITALIC));
compactItem.setColumnSpan(0, 11);
compactItem.setBackground(0, new Color(null, 0,255,0));
addCarRow(car1, compactItem);
addCarRow(car2, compactItem);
addCarRow(car3, compactItem);
GridItem midSizedItem = new GridItem(grid, SWT.NONE);
midSizedItem.setText(0, "Mid-Sized");
midSizedItem.setFont(new Font(null, "Arial", 16, SWT.ITALIC));
midSizedItem.setColumnSpan(0,11);
midSizedItem.setBackground(0, new Color(null, 0, 255, 255));
addCarRow(car4, midSizedItem);
addCarRow(car5, midSizedItem);
shell.setSize(850, 200);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
private static void addCarRow(Car car, GridItem parentItem) {
NumberFormat formatter = NumberFormat.getCurrencyInstance();
GridItem item1 = new GridItem(parentItem, SWT.NONE);
item1.setText(1,car.getYear());
item1.setText(2, car.getMake());
item1.setText(3, car.getModel());
item1.setText(4, String.valueOf(car.getCarNumber()));
item1.setText(5, car.getCarType().toString());
item1.setText(6, String.valueOf(car.getMileage()));
item1.setText(7, car.getColor());
item1.setText(8, formatter.format(car.getDailyRentalFee()));
item1.setText(9, formatter.format(car.calculateTax()));
item1.setText(10, formatter.format(car.getTotalFee()));
item1.setChecked(11, car.isAvailable());
}
} |
运行示例将提供如下所示的结果(这里所示的是其处于收缩状态)。 图 21. GridExample6
|