TableViewer 使用说明
类 TableViewer 在 viewer 包下,要在 TableViewer 这个类的表中显示数据
时 , 要 使 用 内 容 器 和 标 签 器 这 两 个 类 。 内 容 器
(IStructuredContentProvider):它的作用是从集合类中提取对象(在数据库
中这个对象是一条记录);标签器(ITableLableProvider):从内容器中提取到
的对象中提取出一个字段(在数据库中对应于某一列的一个值)。
1 下面是关于 TableViewer 的使用:
第一步:创建一个 TableViewer 对象。
/*SWT.MULTI 代表可以选择多行,SWT.FULL_SELECTION 代表可以选择
整行
SWT.BORDER 代表边框,SWT.V_SCROLL SWT.H_SCROLL 代表滚动条*/
TableViewer tv = new TableViewer(shell, SWT.MULTI |
SWT.BORDER | SWT.FULL_SELECTION | SWT.V_SCROLL |
SWT.H_SCROLL);
第二步:通过表格内含的 Table 对象设置布局方式
Table table = tv.getTable();
table.setHeaderVisible(true); // 显示表头
table.setLinesVisible(true); // 显示表格线
TableLayout layout = new TableLayout(); // 专用于表格的布局
table.setLayout(layout);
第三步:用 TableColumn 类创建表格列
layout.addColumnData(new ColumnWeightData(13));// ID 列 宽 13 像
素
new TableColumn(table, SWT.NONE).setText("ID 号");
评论2