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

时间:2016-09-15 06:04:21

标签: swt jface

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

我认为直到现在我已经做好了一切,但是当我编译代码时,我得到错误:

Error Msg

代码:

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));
}

问题:   如何只显示一列组合框?

1 个答案:

答案 0 :(得分:0)

在您在桌面上设置所有内容之前,您不能进行TableViewer setInput来电。必须在调用setInput之前设置所有内容提供者,标签提供者,列布局,编辑支持....

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