Jef*_*ffV 13 eclipse jface cursor tableviewer
我正在使用TableViewer内容提供商,标签提供商,a ICellModifier和TextCellEditors每列.
当用户选择单元格时,如何添加箭头键导航和单元格编辑?我希望这是一种尽可能自然的行为.
在查看了一些在线示例后,似乎有一种旧的方式(使用a TableCursor)和新的方式(TableCursor不与CellEditors?? 混合).
目前,我TableViewer没有光标只会在第一列滚动.底层SWT表将游标显示为null.
是否有通过键盘TableViewer使用CellEditors和单元格导航的好例子?
谢谢!
不知道有没有好的例子。我使用一组自定义代码来获取我认为在TableViewer. (请注意,我们目前仍以 3.2.2 为目标,因此情况可能有所改善或发生其他变化。)一些亮点:
setCellEditors()在我的TableViewer.对于每个CellEditor控制,我建立了我认为合适的控制TraverseListener。例如,对于文本单元格:
cellEditor = new TextCellEditor(table, SWT.SINGLE | getAlignment());
cellEditor.getControl().addTraverseListener(new TraverseListener() {
    public void keyTraversed(TraverseEvent e) {
        switch (e.detail) {
        case SWT.TRAVERSE_TAB_NEXT:
            // edit next column
            e.doit = true;
            e.detail = SWT.TRAVERSE_NONE;
            break;
        case SWT.TRAVERSE_TAB_PREVIOUS:
            // edit previous column
            e.doit = true;
            e.detail = SWT.TRAVERSE_NONE;
            break;
        case SWT.TRAVERSE_ARROW_NEXT:
            // Differentiate arrow right from down (they both produce the same traversal @*$&#%^)
            if (e.keyCode == SWT.ARROW_DOWN) {
                // edit same column next row
                e.doit = true;
                e.detail = SWT.TRAVERSE_NONE;
            }
            break;
        case SWT.TRAVERSE_ARROW_PREVIOUS:
            // Differentiate arrow left from up (they both produce the same traversal @*$&#%^)
            if (e.keyCode == SWT.ARROW_UP) {
                // edit same column previous row
                e.doit = true;
                e.detail = SWT.TRAVERSE_NONE;
            }
            break;
        }
    }
});
(对于下拉表格单元格,我捕获左右箭头而不是上下箭头。)
我还TraverseListener向TableViewer的 控件添加了一个,其工作是如果有人在选择整行时点击“返回”,则开始单元格编辑。
// This really just gets the traverse events for the TABLE itself.  If there is an active cell editor, this doesn't see anything.
tableViewer.getControl().addTraverseListener(new TraverseListener() {
    public void keyTraversed(TraverseEvent e) {
        if (e.detail == SWT.TRAVERSE_RETURN) {
            // edit first column of selected row
        }
    }
});
现在,我如何精确地控制编辑则是另一回事了。就我而言,我的整体TableViewer(以及其中每一列的表示)松散地包装在一个自定义对象中,其中包含执行上面评论所说的方法。这些方法的实现最终会调用tableViewer.editElement()并检查tableViewer.isCellEditorActive()单元格是否实际上可编辑(因此,如果不可编辑,我们可以跳到下一个可编辑单元格)。
我还发现能够以编程方式“放弃编辑”(例如,从一行中的最后一个单元格跳出时)很有用。不幸的是,我能想到的唯一方法是一个可怕的黑客,决定通过深入探究源代码来使用我的特定版本,以找到会产生所需“副作用”的东西:
    private void relinquishEditing() {
        // OMG this is the only way I could find to relinquish editing without aborting.
        tableViewer.refresh("some element you don't have", false);
    }
抱歉,我无法提供更完整的代码块,但实际上,我必须发布一个完整的小型项目,而我现在还不准备这样做。希望这足以“快速启动”让您继续前进。