SWT使用TAB移动TableViewer

时间:2012-12-06 16:00:46

标签: widget swt jface tableviewer

Another similar StackOverflow question帮助了我。

我的TableViewer单元格包含布尔小部件editors(不是通常的检查按钮小部件,而是一个切换按钮,可以是“已选中”/“未选中”)。




表必须:
- 在选择单元格时始终显示编辑器;
- 当细胞焦点丢失时禁用编辑器;
- 按下row时正确移动到下一个TAB;




显然很容易,在实践中很难。很多听众的冲突(TAB并没有真正奏效)。

稍后编辑:

我已经设法解决了大多数错误,但是这个错误仍然让我大吃一惊。如果我使用鼠标更改窗口小部件的值,然后按TAB进行遍历,焦点将跳转到第二行,而不是下一行。但是,如果我使用空格键更改编辑器的值,那么遍历工作正常;它应该跳到下一行。

我应该如何调试那些该死的听众?

1 个答案:

答案 0 :(得分:5)

为了便于开发,您可以在JFace中使用一些帮助程序类,而不必处理Tab键或鼠标单击的事件处理。以下是一个可以帮助您入门的代码段:

TableViewer viewer = ...

TableViewerFocusCellManager focusCellManager = new TableViewerFocusCellManager(viewer, new FocusCellOwnerDrawHighlighter(viewer));

ColumnViewerEditorActivationStrategy activationSupport = new ColumnViewerEditorActivationStrategy(viewer) {
    protected boolean isEditorActivationEvent(ColumnViewerEditorActivationEvent event) {
        if (event.eventType == ColumnViewerEditorActivationEvent.MOUSE_CLICK_SELECTION) {
            EventObject source = event.sourceEvent;
            if (source instanceof MouseEvent && ((MouseEvent)source).button == 3)
                return false;
        }
        return super.isEditorActivationEvent(event) || (event.eventType == ColumnViewerEditorActivationEvent.KEY_PRESSED && event.keyCode == SWT.CR);
    }
};

TableViewerEditor.create(viewer, focusCellManager, activationSupport, ColumnViewerEditor.TABBING_HORIZONTAL | 
    ColumnViewerEditor.TABBING_MOVE_TO_ROW_NEIGHBOR | 
    ColumnViewerEditor.TABBING_VERTICAL |
    ColumnViewerEditor.KEYBOARD_ACTIVATION);