I am using a TableViewer
with a content provider, label provider, a ICellModifier
and TextCellEditors
for each column.
我正在为每列使用带有内容提供程序,标签提供程序,ICellModifier和TextCellEditors的TableViewer。
How can I add arrow key navigation and cell editing when the user selects the cell? I would like this to be as natural a behavior as possible.
当用户选择单元格时,如何添加箭头键导航和单元格编辑?我希望这是一种尽可能自然的行为。
After looking at some of the online examples, there seems to be an old way (with a TableCursor
) and a new way (TableCursor
does not mix with CellEditors
??).
在查看了一些在线示例后,似乎有一种旧方法(使用TableCursor)和一种新方式(TableCursor不与CellEditors混合使用)。
Currently, my TableViewer
without a cursor will scroll in the first column only. The underlying SWT table is showing cursor as null.
目前,没有光标的TableViewer将仅在第一列中滚动。底层SWT表将游标显示为null。
Is there a good example of TableViewer
using CellEditors
and cell navigation via keyboard?
TableViewer是否有一个使用CellEditors和通过键盘进行单元格导航的好例子?
Thanks!
5 个解决方案
#1
I don't know if there is a good example. I use a cluster of custom code to get what I would consider to be basic table behaviors for my application working on top of TableViewer
. (Note that we are still targetting 3.2.2 at this point, so maybe things have gotten better or have otherwise changed.) Some highlights:
我不知道是否有一个很好的例子。我使用一组自定义代码来获得我认为是在TableViewer上运行的应用程序的基本表行为。 (请注意,此时我们仍然以3.2.2为目标,所以可能事情变得更好或者有所改变。)一些亮点:
- I do
setCellEditors()
on myTableViewer
. -
On each
CellEditor
's control, I establish what I consider to be an appropriateTraverseListener
. For example, for text cells:在每个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; } } });
我在TableViewer上设置了setCellEditors()。
(For drop-down table cells, I catch left and right arrow instead of up and down.)
(对于下拉表单元格,我捕获左右箭头而不是上下。)
-
I also add a
TraverseListener
to theTableViewer
's control whose job it is to begin cell editing if someone hits "return" while an entire row is selected.我还将一个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 } } });
-
Now, how exactly I control the editing is another story. In my case, my whole
TableViewer
(and a representation of each column therein) is loosely wrapped up in a custom object with methods to do what the comments above say. The implementations of those methods ultimately end up callingtableViewer.editElement()
and then checkingtableViewer.isCellEditorActive()
to see if the cell was actually editable (so we can skip to the next editable one if not).现在,我如何控制编辑是另一个故事。在我的例子中,我的整个TableViewer(以及其中每个列的表示)松散地包含在一个自定义对象中,其方法可以执行上述注释所述的操作。这些方法的实现最终最终调用tableViewer.editElement(),然后检查tableViewer.isCellEditorActive()以查看该单元格是否实际可编辑(因此,如果没有,我们可以跳到下一个可编辑的单元格)。
-
I also found it useful to be able to programmatically "relinquish editing" (e.g. when tabbing out of the last cell in a row). Unfortunately the only way I could come up with to do that is a terrible hack determined to work with my particular version by spelunking through the source for things that would produce the desired "side effects":
我还发现能够以编程方式“放弃编辑”(例如,当连续排出最后一个单元格时)是有用的。不幸的是,我能做到这一点的唯一方法是一个可怕的黑客决定使用我的特定版本,通过搜索源会产生所需的“副作用”:
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); }
Sorry I can't give a more complete chunk of code, but really, I'd have to release a whole mini-project of stuff, and I'm not prepared to do that now. Hopefully this is enough of a "jumpstart" to get you going.
对不起,我无法提供更完整的代码,但实际上,我必须发布一个完整的小项目,我现在还没准备好。希望这足以让你开始“快速启动”。
#2
Here is what has worked for me:
这对我有用:
TableViewerFocusCellManager focusCellManager = new TableViewerFocusCellManager(tableViewer,new FocusCellOwnerDrawHighlighter(tableViewer));
ColumnViewerEditorActivationStrategy actSupport = new ColumnViewerEditorActivationStrategy(tableViewer) {
protected boolean isEditorActivationEvent(ColumnViewerEditorActivationEvent event) {
return event.eventType == ColumnViewerEditorActivationEvent.TRAVERSAL
|| event.eventType == ColumnViewerEditorActivationEvent.MOUSE_DOUBLE_CLICK_SELECTION
|| (event.eventType == ColumnViewerEditorActivationEvent.KEY_PRESSED && event.keyCode == SWT.CR)
|| event.eventType == ColumnViewerEditorActivationEvent.PROGRAMMATIC;
}
};
I can navigate in all directions with tab while editing, and arrow around when not in edit mode.
我可以在编辑时使用标签在所有方向导航,在不在编辑模式时使用箭头。
#4
I got it working based on this JFace Snippet, but I had to copy a couple of related classes also:
我基于这个JFace Snippet工作了,但我还要复制几个相关的类:
- org.eclipse.jface.snippets.viewers.TableCursor
- org.eclipse.jface.snippets.viewers.CursorCellHighlighter
- org.eclipse.jface.snippets.viewers.AbstractCellCursor
and I don't remember exactly where I found them. The is also a org.eclipse.swt.custom.TableCursor, but I couldn't get that to work.
而且我不记得我找到它们的确切位置。这也是一个org.eclipse.swt.custom.TableCursor,但我无法让它工作。
#5
Have a look at Example of enabling Editor Activation on a Double Click.
查看双击启用编辑器激活的示例。
The stuff between lines [ 110 - 128 ] add a ColumnViewerEditorActivationStrategy and TableViewerEditor. In my case the I wanted a single click to begin editing so i changed line 115 from: ColumnViewerEditorActivationEvent.MOUSE_DOUBLE_CLICK_SELECTION to ColumnViewerEditorActivationEvent.MOUSE_CLICK_SELECTION. After adding this to my TableViewer, the tab key would go from field to field with the editor enabled.
行[110 - 128]之间的东西添加了ColumnViewerEditorActivationStrategy和TableViewerEditor。在我的情况下,我只需要点击一下即可开始编辑,所以我将第115行从ColumnViewerEditorActivationEvent.MOUSE_DOUBLE_CLICK_SELECTION更改为ColumnViewerEditorActivationEvent.MOUSE_CLICK_SELECTION。将此添加到我的TableViewer后,Tab键将在启用编辑器的情况下从一个字段转到另一个字段。
#1
I don't know if there is a good example. I use a cluster of custom code to get what I would consider to be basic table behaviors for my application working on top of TableViewer
. (Note that we are still targetting 3.2.2 at this point, so maybe things have gotten better or have otherwise changed.) Some highlights:
我不知道是否有一个很好的例子。我使用一组自定义代码来获得我认为是在TableViewer上运行的应用程序的基本表行为。 (请注意,此时我们仍然以3.2.2为目标,所以可能事情变得更好或者有所改变。)一些亮点:
- I do
setCellEditors()
on myTableViewer
. -
On each
CellEditor
's control, I establish what I consider to be an appropriateTraverseListener
. For example, for text cells:在每个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; } } });
我在TableViewer上设置了setCellEditors()。
(For drop-down table cells, I catch left and right arrow instead of up and down.)
(对于下拉表单元格,我捕获左右箭头而不是上下。)
-
I also add a
TraverseListener
to theTableViewer
's control whose job it is to begin cell editing if someone hits "return" while an entire row is selected.我还将一个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 } } });
-
Now, how exactly I control the editing is another story. In my case, my whole
TableViewer
(and a representation of each column therein) is loosely wrapped up in a custom object with methods to do what the comments above say. The implementations of those methods ultimately end up callingtableViewer.editElement()
and then checkingtableViewer.isCellEditorActive()
to see if the cell was actually editable (so we can skip to the next editable one if not).现在,我如何控制编辑是另一个故事。在我的例子中,我的整个TableViewer(以及其中每个列的表示)松散地包含在一个自定义对象中,其方法可以执行上述注释所述的操作。这些方法的实现最终最终调用tableViewer.editElement(),然后检查tableViewer.isCellEditorActive()以查看该单元格是否实际可编辑(因此,如果没有,我们可以跳到下一个可编辑的单元格)。
-
I also found it useful to be able to programmatically "relinquish editing" (e.g. when tabbing out of the last cell in a row). Unfortunately the only way I could come up with to do that is a terrible hack determined to work with my particular version by spelunking through the source for things that would produce the desired "side effects":
我还发现能够以编程方式“放弃编辑”(例如,当连续排出最后一个单元格时)是有用的。不幸的是,我能做到这一点的唯一方法是一个可怕的黑客决定使用我的特定版本,通过搜索源会产生所需的“副作用”:
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); }
Sorry I can't give a more complete chunk of code, but really, I'd have to release a whole mini-project of stuff, and I'm not prepared to do that now. Hopefully this is enough of a "jumpstart" to get you going.
对不起,我无法提供更完整的代码,但实际上,我必须发布一个完整的小项目,我现在还没准备好。希望这足以让你开始“快速启动”。
#2
Here is what has worked for me:
这对我有用:
TableViewerFocusCellManager focusCellManager = new TableViewerFocusCellManager(tableViewer,new FocusCellOwnerDrawHighlighter(tableViewer));
ColumnViewerEditorActivationStrategy actSupport = new ColumnViewerEditorActivationStrategy(tableViewer) {
protected boolean isEditorActivationEvent(ColumnViewerEditorActivationEvent event) {
return event.eventType == ColumnViewerEditorActivationEvent.TRAVERSAL
|| event.eventType == ColumnViewerEditorActivationEvent.MOUSE_DOUBLE_CLICK_SELECTION
|| (event.eventType == ColumnViewerEditorActivationEvent.KEY_PRESSED && event.keyCode == SWT.CR)
|| event.eventType == ColumnViewerEditorActivationEvent.PROGRAMMATIC;
}
};
I can navigate in all directions with tab while editing, and arrow around when not in edit mode.
我可以在编辑时使用标签在所有方向导航,在不在编辑模式时使用箭头。
#3
#4
I got it working based on this JFace Snippet, but I had to copy a couple of related classes also:
我基于这个JFace Snippet工作了,但我还要复制几个相关的类:
- org.eclipse.jface.snippets.viewers.TableCursor
- org.eclipse.jface.snippets.viewers.CursorCellHighlighter
- org.eclipse.jface.snippets.viewers.AbstractCellCursor
and I don't remember exactly where I found them. The is also a org.eclipse.swt.custom.TableCursor, but I couldn't get that to work.
而且我不记得我找到它们的确切位置。这也是一个org.eclipse.swt.custom.TableCursor,但我无法让它工作。
#5
Have a look at Example of enabling Editor Activation on a Double Click.
查看双击启用编辑器激活的示例。
The stuff between lines [ 110 - 128 ] add a ColumnViewerEditorActivationStrategy and TableViewerEditor. In my case the I wanted a single click to begin editing so i changed line 115 from: ColumnViewerEditorActivationEvent.MOUSE_DOUBLE_CLICK_SELECTION to ColumnViewerEditorActivationEvent.MOUSE_CLICK_SELECTION. After adding this to my TableViewer, the tab key would go from field to field with the editor enabled.
行[110 - 128]之间的东西添加了ColumnViewerEditorActivationStrategy和TableViewerEditor。在我的情况下,我只需要点击一下即可开始编辑,所以我将第115行从ColumnViewerEditorActivationEvent.MOUSE_DOUBLE_CLICK_SELECTION更改为ColumnViewerEditorActivationEvent.MOUSE_CLICK_SELECTION。将此添加到我的TableViewer后,Tab键将在启用编辑器的情况下从一个字段转到另一个字段。