断言失败:未知的列布局数据

assertion failed: Unknown column layout data

当我在 Eclipse SWT 中使用 table 查看器创建组合框时,特别是只有第 3 列。

我认为到目前为止我所做的一切都很好,但是当我编译代码时出现错误:

代码:

public void createPartControl(Composite parent) {

    Composite tableComposite = new Composite(parent, SWT.NONE);
    tableColumnLayout = new TableColumnLayout();
    tableComposite.setLayout(tableColumnLayout);
    tableComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
            true));

    tableViewer = new TableViewer(tableComposite, SWT.MULTI | SWT.H_SCROLL
            | SWT.V_SCROLL);
    tableViewer.setContentProvider(ArrayContentProvider.getInstance());
    // TODO viewer.setLabelProvider(new ViewLabelProvider());
    table = tableViewer.getTable();
    // Table table = tableViewer.getTable();
    table.setHeaderVisible(true);
    table.setLinesVisible(true);

    String[] titles = { "Threat Name", "Category Name", "Status",
            "Priority", "Description", "Justification" };
     int[] bounds = { 100, 100, 100, 100 };
    TableViewerColumn col = createTableViewerColumn(titles[2], bounds[2], 2);
    col.setLabelProvider(new ColumnLabelProvider() {
        @Override
        public String getText(Object element) {
            Dummy p = (Dummy) element;
            return p.getValue();
        }
    });
    col.setEditingSupport(new FirstValueEditingSupport(tableViewer));

}


private SelectionAdapter getSelectionAdapter(final TableColumn column,
        final int index) {
    SelectionAdapter selectionAdapter = new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            tableViewer.refresh();
        }
    };
    return selectionAdapter;
}

private static class Dummy {
    public String value;

    public Dummy(String value) {
        this.value = value;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

public static class FirstValueEditingSupport extends EditingSupport {

    private final TableViewer viewer;
    private final CellEditor editor;

    private final String[] possibleValues = { "Mitigated",
            "Not Applicable", "Not Started", "Needs Investigation" };

    public FirstValueEditingSupport(TableViewer viewer) {
        super(viewer);
        this.viewer = viewer;
        this.editor = new ComboBoxCellEditor(viewer.getTable(),
                possibleValues);
    }

    @Override
    protected CellEditor getCellEditor(Object element) {
        return editor;
    }

    @Override
    protected boolean canEdit(Object element) {
        return true;
    }

    @Override
    protected Object getValue(Object element) {
        Dummy dummy = (Dummy) element;

        int index = 0;

        for (int i = 0; i < possibleValues.length; i++) {
            if (Objects.equals(possibleValues[i], dummy.getValue())) {
                index = i;
                break;
            }
        }

        return index;
    }

    @Override
    protected void setValue(Object element, Object value) {
        Dummy dummy = (Dummy) element;

        int index = (Integer) value;

        dummy.setValue(possibleValues[index]);

        viewer.update(element, null);
    }
}

private void fillRows(String shortdesc, String categ, String descp) {
    System.out.println("fillRows call from above method.");
    TableColumn status_Name_Col = tableViewer.getTable().getColumn(2);
    System.out.println("**************** status_Name_Col ************ "
            + status_Name_Col);

    tableViewer
            .addSelectionChangedListener(new ISelectionChangedListener() {
                @Override
                public void selectionChanged(
                        SelectionChangedEvent selectionChangedEvent) {
                    StructuredSelection selection = (StructuredSelection) selectionChangedEvent
                            .getSelection();
                    System.out.println(((Dummy) selection.getFirstElement())
                            .getValue());
                }
            });

    List<Dummy> elements = new ArrayList<>();
    for (int i = 0; i < Connection.Number_Of_Connection; i++) {
        elements.add(new Dummy("First option"));
    }
    tableViewer.setInput(elements);
    tableColumnLayout.setColumnData(status_Name_Col, new ColumnWeightData(
            1, true));
    tableViewerColumn.setEditingSupport(new FirstValueEditingSupport(
            tableViewer));
}

问题: 如何显示组合框,特别是只有一列?

在 table 上设置 一切 之前,您不得调用 TableViewer setInput。所有的内容提供者、标签提供者、列布局、编辑支持……必须在调用 setInput 之前设置。

您还必须为您创建的每个列调用 setColumnData