表是支持TableColumns 的列表的增强形式。这些列将它们的数据对齐成一种更可读的形式。它们还支持列名,并能调整列的大小。要创建表,首先要创建表控件,然后添加 TableItems 中包装的字符串数据。
表支持以下可选样式:
CHECK 将复选框添加到第一列中。
VIRTUAL 支持大型表(特定于平台)。
FULL_SELECTION 选择所有列(不仅仅是第一列)。
清单 6 创建了图 6 中所示的表。
清单 6. 使用 helper 方法创建一个表
1 // Create the Table and TableColumns
2 protected Table createTable(Composite parent, int mode, Object[] contents) {
3 table = new Table(parent, mode | SWT.SINGLE | SWT.FULL_SELECTION |
4 SWT.V_SCROLL | SWT.H_SCROLL);
5 table.setHeaderVisible(true);
6 table.setLinesVisible(true);
7 createTableColumn(table, SWT.LEFT, "Column 1", 100);
8 createTableColumn(table, SWT.CENTER, "Column 2", 100);
9 createTableColumn(table, SWT.RIGHT, "Column 3", 100);
10 addTableContents(contents);
11 return table;
12 }
13 protected TableColumn createTableColumn(Table table, int style, String title, int width) {
14 TableColumn tc = new TableColumn(table, style);
15 tc.setText(title);
16 tc.setResizable(true);
17 tc.setWidth(width);
18 return tc;
19 }
20 protected void addTableContents(Object[] items) {
21 for (int i = 0; i < items.length; i++) {
22 String[] item = (String[])items[i];
23 TableItem ti = new TableItem(table, SWT.NONE);
24 ti.setText(item);
25 }
26 }
27 :
28 // sample creation code
29 protected void initGui() {
30 Object[] items = {
31 new String[] {"A", "a", "0"}, new String[] {"B", "b", "1"},
32 new String[] {"C", "c", "2"}, new String[] {"D", "d", "3"},
33 new String[] {"E", "e", "4"}, new String[] {"F", "f", "5"},
34 new String[] {"G", "g", "6"}, new String[] {"H", "h", "7"},
35 new String[] {"I", "i", "8"}, new String[] {"J", "j", "9"}
36 };
37 table = createTable(this, SWT.CHECK, items);
38 }
39
2 protected Table createTable(Composite parent, int mode, Object[] contents) {
3 table = new Table(parent, mode | SWT.SINGLE | SWT.FULL_SELECTION |
4 SWT.V_SCROLL | SWT.H_SCROLL);
5 table.setHeaderVisible(true);
6 table.setLinesVisible(true);
7 createTableColumn(table, SWT.LEFT, "Column 1", 100);
8 createTableColumn(table, SWT.CENTER, "Column 2", 100);
9 createTableColumn(table, SWT.RIGHT, "Column 3", 100);
10 addTableContents(contents);
11 return table;
12 }
13 protected TableColumn createTableColumn(Table table, int style, String title, int width) {
14 TableColumn tc = new TableColumn(table, style);
15 tc.setText(title);
16 tc.setResizable(true);
17 tc.setWidth(width);
18 return tc;
19 }
20 protected void addTableContents(Object[] items) {
21 for (int i = 0; i < items.length; i++) {
22 String[] item = (String[])items[i];
23 TableItem ti = new TableItem(table, SWT.NONE);
24 ti.setText(item);
25 }
26 }
27 :
28 // sample creation code
29 protected void initGui() {
30 Object[] items = {
31 new String[] {"A", "a", "0"}, new String[] {"B", "b", "1"},
32 new String[] {"C", "c", "2"}, new String[] {"D", "d", "3"},
33 new String[] {"E", "e", "4"}, new String[] {"F", "f", "5"},
34 new String[] {"G", "g", "6"}, new String[] {"H", "h", "7"},
35 new String[] {"I", "i", "8"}, new String[] {"J", "j", "9"}
36 };
37 table = createTable(this, SWT.CHECK, items);
38 }
39
图 6. 表的例子

第一列中的复选框是可选的。注意列的对齐方式。