CheckboxTableViewer 的设置 header

Setting header of CheckboxTableViewer

CheckboxTableViewer 允许创建单个清单。

但是我如何放置这样一个列的header,因为该列本身不是由 TableColumn 创建的。

    tableViewer = CheckboxTableViewer.newCheckList(parent, SWT.SINGLE| SWT.BORDER | SWT.FULL_SELECTION);

    tableViewer.add(checkListNames.get(0));
    tableViewer.add(checkListNames.get(1));
    tableViewer.add(checkListNames.get(2));
    tableViewer.add(checkListNames.get(3));
    tableViewer.add(checkListNames.get(4));
    tableViewer.add(checkListNames.get(5));

    final Table table = tableViewer.getTable();

    table.setLayoutData(new GridData(GridData.FILL_BOTH));

    System.out.println(table.getColumnCount()); // this returns a zero

    table.setHeaderVisible(true);
    table.setLinesVisible(true);

您需要使用 TableLayoutTableViewerColumn 定义列,以便您可以设置 header 文本。

最小代码为:

TableLayout layout = new TableLayout();

TableViewerColumn col = new TableViewerColumn(tableViewer, SWT.LEAD);

col.getColumn().setText("Text");

layout.addColumnData(new ColumnWeightData(100));

table.setLayout(layout);