gdwrx_winson 发表于 2013-1-28 19:13:40

Eclipse Nebula 部件入门

转自https://www6.software.ibm.com/developerworks/cn/education/opensource/os-eclipse-nebula/section13.html
 
http://www.agoit.com/bbs/https://www.ibm.com/i/c.gif
developerWorks 中国  >  Open source  >http://www.agoit.com/bbs/https://www.ibm.com/i/c.gif
Eclipse Nebula 部件入门

Eclipse Nebula 的 Grid、CDateTime、CompositeTable、PGroup 和 PShelf 部件快速入门指南
http://www.agoit.com/bbs/https://www.ibm.com/i/c.gifhttp://www.agoit.com/bbs/https://www6.software.ibm.com/developerworks/i/dw.gifhttp://www.agoit.com/bbs/https://www.ibm.com/i/c.gifhttp://www.agoit.com/bbs/https://www.ibm.com/i/c.gifhttp://www.agoit.com/bbs/https://www.ibm.com/i/v14/buttons/arrow_lt.gif第 13 页,共 21 页http://www.agoit.com/bbs/https://www.ibm.com/i/v14/buttons/arrow_rd.gifhttp://www.agoit.com/bbs/https://www.ibm.com/i/c.gif
文档选项http://www.agoit.com/bbs/https://www.ibm.com/i/c.gifhttp://www.agoit.com/bbs/https://www.ibm.com/i/v14/icons/printer.gif打印本页

对本教程的评价http://www.agoit.com/bbs/https://www.ibm.com/i/c.gifhttp://www.agoit.com/bbs/https://www.ibm.com/i/v14/icons/d_bold.gif帮助我们改进这些内容

复选框、行标题、更改单元格颜色和单元格选择
行标题使您可以在 Grid 中设定每行的标签。默认情况下,每行的标题在 Grid 中是行的序号。要显示行标题,请在 Grid 上调用 setRowHeaderVisible(true)。每个 GridItem 都可以指定它自己的标题。通过在条目上调用 setHeaderText("Header text") 完成此过程。
您可以指定 GridColumn 作为包含一个复选框的列,呈现 Boolean 字段的值时该复选框会十分有用。下面的代码将创建一个包含复选框的 GridColumn。
GridColumn availableColumn = new GridColumn(grid, SWT.CHECK);
Grid 的特定单元格中的复选框的选中状态是由其相关的 GridItem 确定的。您可以通过调用相应的 GridItem 的 setChecked(int, boolean) 方法并传递列的索引和所需的 Boolean 值来设定状态。您可以通过在 GridItem 上调用 getChecked(int) 方法来检索复选框的值。文本也可以显示在复选框单元格中。这可以通过在 GridItem 上调用 setText(int, String) 方法并传递复选框条目的列的索引和将要显示的文本来完成。
您可以单独更改背景和文本的颜色,以及 Grid 中每个单元格的文本的字体。为此,在相应的 GridItem 上调用清单 5 中的方法。

清单 5. 更改颜色和字体
                  item.setBackground(index, Color)item.setFont(index, Font); item.setForeground(index, Color);
可以启用类似电子表单行为的 Grid 的优秀特性是能够启用单元格选择。可以选择单个单元格、一组单元格、整列和整行。默认情况下,Grid 被设为允许一次选择一行。选择多行可由 OR SWT.MULTI 以及 Grid 构造函数中提供的其余样式整数来指定。示例如下所示:
Grid grid = new Grid(shell, SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL);
要启用选择单个单元格或单元格组,请在 Grid 上调用 setCellSelectionEnabled(true)。默认情况下,所有列中的单元格都是可选择的。要禁止选择列中的单元格,请在相应的 GridColumn 上调用 setCellSelectionEnabled 方法并向其传递 false 值。要确定哪些单元格目前处于选中状态,请在 Grid 上调用 getCellSelection() 方法。将返回包含每个选中单元格坐标的 Point 对象数组。
清单 6 将演示如何使用行标题、复选框、单元格颜色和单元格选择。尝试执行单元格选择。单击列标题应当选择该列中的所有单元格。单击行标题应当选择该行中的所有单元格。注:第一列 Car Number 中的所有单元格应当都不会被选中。

清单 6. GridExample2
                  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 Headersgrid.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 selectedidColumn.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 checkeditem1.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 blueitem2.setBackground(4,new Color(null, 0,0,255)); //change fontitem2.setFont(4, new Font(null, "Arial", 12, SWT.BOLD | SWT.ITALIC));//set text color to reditem2.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();}}
在 Mac OS X 中运行示例将提供如下所示的结果。

图 17. GridExample2
http://www.agoit.com/bbs/https://www6.software.ibm.com/developerworks/cn/education/opensource/os-eclipse-nebula/figure17.jpg 
页: [1]
查看完整版本: Eclipse Nebula 部件入门