Eclipse插件开发系列6.TableViewer的使用(1)让数据显示出来(3) public class App1 {
public static void main(String[] args) { App1 window = new App1(); window.open(); } public void open() { final Display display = new Display(); final Shell shell = new Shell(); shell.setLayout(new FillLayout()); shell.setText("SWT Application"); { TableViewer tv = new TableViewer(shell, SWT.MULTI SWT.H_SCROLL SWT.V_SCROLL SWT.BORDER SWT.FULL_SELECTION); /* * TableViewer是通过Table来布局的 */ Table table = tv.getTable(); //table.setLayoutData(new GridData(GridData.FILL_BOTH)); table.setHeaderVisible(true); //表头显示 table.setLinesVisible(true); //表格线显示 /* * 建立表格中的列 */ TableLayout tLayout = new TableLayout(); table.setLayout(tLayout); tLayout.addColumnData(new ColumnWeightData(10)); //10个单位的宽度 new TableColumn(table, SWT.NONE).setText("ID号"); tLayout.addColumnData(new ColumnWeightData(40)); new TableColumn(table, SWT.NONE).setText("姓名"); tLayout.addColumnData(new ColumnWeightData(20)); new TableColumn(table, SWT.NONE).setText("年龄"); tLayout.addColumnData(new ColumnWeightData(60)); new TableColumn(table, SWT.NONE).setText("记录建立时间"); /* * 当People的记录集输入的tv中,注意setInput的参数是Object也就是说它可以接受任何参数, * 不过它最常接受的还是Java的Collection(集合,这里为List)或者数组, * 那么tv是怎么知道如何来显示这么输入格式千差万别的数据的呢?就是依靠内容器和标签器(这里它两是一个内部类) */ tv.setContentProvider(new MyContentProvider()); //内容器 tv.setLabelProvider(new MyLabelProvider()); //标签器 tv.setInput(getPeoples());