我目前正在开发Eclipse4 RCP应用程序,并尝试获得具有两列的JFace TableViewer,其中一列上具有可选的ComboBoxCellEditor
以下是一个大问题:如果我从ComboBox中选择一个值,则该列的Label不变。如果再次单击组合框,它将显示应显示的值。
因此,例如,如果我可以选择“ Expr”和“ String”,而当前选择是“ Expr”,则如果我从Combo中选择“ String”并单击其他位置,则Combo仍显示“ Expr”。如果我再次单击ComboBox并将其聚焦,它将显示“ String”。如果我再次单击其他位置,它仍然显示“ Expr”。
我使用几个类来使Table运行。

我的模特班:



private String element;
private String type;

public TableModelElement(String element, String type) {
    this.element = element;
    this.type = type;
}

public TableModelElement(String element) {
    this(element, SetTypeEnum.SET_TYPE_STRING.toString());
}

public TableModelElement() {
    this("", SetTypeEnum.SET_TYPE_STRING.toString());
}
//And Getter/Setter


我的SetTypeEnum:

public enum SetTypeEnum {
SET_TYPE_EXPR("expr"), SET_TYPE_STRING("string");

private final String setType;

private SetTypeEnum(String setType) {
    this.setType = setType;
}

@Override
public String toString() {
    return setType;
}

public static String[] toArray() {
    String[] values = new String[SetTypeEnum.values().length];
    int i = 0;
    for (SetTypeEnum value : SetTypeEnum.values()) {
        if (i < values.length) {
            values[i] = value.toString();
            i++;
        }
    }
    return values;
}
}


TableViewer启动器:

public class SetTable {

    private TableViewer viewer;
    private TableLayout layout;
    private TableViewerColumn colElementText;
    private TableViewerColumn colElementType;

    public SetTable(Composite parent) {
        layout = new TableLayout();
        layout.addColumnData(new ColumnWeightData(3));
        layout.addColumnData(new ColumnWeightData(1));
        Table table = new Table(parent, SWT.BORDER | SWT.FULL_SELECTION
                | SWT.V_SCROLL);
        table.setLinesVisible(true);
        table.setHeaderVisible(true);
        table.setLayout(layout);
        viewer = new TableViewer(table);
        viewer.getControl().setLayoutData(
                new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
        colElementText = new TableViewerColumn(viewer, SWT.NONE);
        colElementText.getColumn().setResizable(true);
        colElementText.getColumn().setText("Wert");
        colElementType = new TableViewerColumn(viewer, SWT.NONE);
        colElementType.getColumn().setResizable(false);
        colElementType.getColumn().setText("Typ");
        EditingSupport exampleEditingSupport = new SetTableEditingSupport(
                colElementType.getViewer());
        colElementType.setEditingSupport(exampleEditingSupport);
        viewer.setContentProvider(new ArrayContentProvider());
        viewer.setLabelProvider(new SetTableLabelProvider());

        TableModelElement[] model = new TableModelElement[] {
                new TableModelElement("Test1",
                        SetTypeEnum.SET_TYPE_EXPR.toString()),
                new TableModelElement("Test2",
                        SetTypeEnum.SET_TYPE_EXPR.toString()) };
        viewer.setInput(model);
    }


和EditingSupport:

public class SetTableEditingSupport extends EditingSupport {

    private ComboBoxViewerCellEditor cellEditor = null;

    public SetTableEditingSupport(ColumnViewer colViewer) {
        super(colViewer);
        cellEditor = new ComboBoxViewerCellEditor((Composite) getViewer()
                .getControl(), SWT.READ_ONLY);
        cellEditor.setLabelProvider(new LabelProvider());
        cellEditor.setContentProvider(new ArrayContentProvider());
        cellEditor.setInput(SetTypeEnum.toArray());
    }

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

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

    @Override
    protected Object getValue(Object element) {
        if (element instanceof TableModelElement) {
            TableModelElement data = (TableModelElement) element;
            return data.getType();
        }
        return null;
    }

    @Override
    protected void setValue(Object element, Object type) {
        if (element instanceof TableModelElement && type instanceof String) {
            TableModelElement data = (TableModelElement) element;
            String newType = (String) type;
            /* only set new value if it differs from old one */
            if (!data.getType().equals(newType)) {
                data.setType(newType);
//              String[] items =((CCombo)cellEditor.getControl()).getItems();
//              for(int i=0;i<items.length;i++) {
//                  if(items[i].equalsIgnoreCase(newType)) {
//                      System.out.println("Will select Combo index i=" + i);
//                      ((CCombo)cellEditor.getControl()).select(i);
//                  }
//              }
            }
        }
    }
}


在注释的部分中,我尝试手动设置“组合值”,但是它不起作用。我认为所选值甚至可能不是问题,就像我将焦点放在ComboBox上一样,显示了正确的值。我还尝试向ComboBoxCellEditor添加一个自定义LabelProvider,但这也不会改变其行为(这并不奇怪,因为标准实现使用toString方法)

最佳答案

在编辑支持setValue方法调用中:



getViewer().update(element, null);


在通话后输入以下内容:

data.setType(newType);


这告诉表查看器它需要更新内容提供者中的元素。

关于java - ComboBoxCellEditor不会更改标签,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26672685/

10-12 02:04