Setting focus cell in TableViewer

I would like to find a way to set focus to a cell in a multi-column table
by an internal Process other than the activation events listed in the code
below. One would expect that the setFocusCell(viewerCell) from
TableViewerFocusCellManager from SWTFocusCellManager would get the job
done, but it is not visible.

Suggestions for setting focus and highlighting a cell based on an internal
event?

Here's the set-up code for the TableViewer and related entities:

tableViewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | 
SWT.V_SCROLL 
| SWT.BORDER | SWT.FULL_SELECTION); 
final Table table = tableViewer.getTable(); 
final TableViewerFocusCellManager mgr = new 
TableViewerFocusCellManager(tableViewer, new 
FocusCellOwnerDrawHighlighter(tableViewer)); 
final ColumnViewerEditorActivationStrategy actSupport = new 
ColumnViewerEditorActivationStrategy(tableViewer) { 
@Override 
protected boolean isEditorActivationEvent 
(ColumnViewerEditorActivationEvent event) { 
return event.eventType == 
ColumnViewerEditorActivationEvent.TRAVERSAL 
|| event.eventType == 
ColumnViewerEditorActivationEvent.MOUSE_CLICK_SELECTION 
|| event.eventType == 
ColumnViewerEditorActivationEvent.KEY_PRESSED 
&& (event.keyCode == SWT.CR || event.character == ' ') 
|| event.eventType == 
ColumnViewerEditorActivationEvent.PROGRAMMATIC; 
} 
}; 

TableViewerEditor.create(tableViewer, mgr, actSupport, 
ColumnViewerEditor.TABBING_HORIZONTAL 
| ColumnViewerEditor.TABBING_MOVE_TO_ROW_NEIGHBOR | 
ColumnViewerEditor.TABBING_VERTICAL 
| ColumnViewerEditor.KEYBOARD_ACTIVATION); 

TableViewerColumn column; 
final int[] styles = { SWT.LEAD, SWT.LEAD, SWT.LEAD, SWT.LEAD, SWT.LEAD, 
SWT.LEAD, SWT.LEAD}; 
for (int columnIndex = 0; columnIndex columnIndex++) { 
column = new TableViewerColumn(tableViewer, styles[columnIndex]); 
// etc. for each column, specifying setLabelProvider and 
setEditingSupport

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用 TableViewer 中的 `addFilter` 方法来实现搜索列值的功能。首先,你需要创建一个实现了 `IViewerFilter` 接口的过滤器类。在该类中,你需要实现 `select` 方法来判断给定元素是否应该显示在表格中。在 `select` 方法中,你可以通过传入的参数 `element` 来访问表格中的每一行数据。然后,你可以使用 `TableItem` 对象的 `getText` 方法来获取该行中每个单元格的文本值。最后,你可以将搜索关键字与单元格文本值进行比较,如果匹配则返回 `true`,否则返回 `false`。 接下来,你可以将该过滤器类实例添加到 TableViewer 中,以便在搜索时调用。你可以使用 `addFilter` 方法将过滤器添加到 TableViewer 中。当用户输入搜索关键字时,你可以在 Text 控件的 `ModifyListener` 中更新过滤器的搜索关键字,并调用 `refresh` 方法重新加载数据以显示匹配的结果。 以下是一个示例代码片段,其中 `MyFilter` 是实现了 `IViewerFilter` 接口的过滤器类: ``` // 创建一个 TableViewer 实例 TableViewer viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION); // 设置 TableViewer 的内容提供者和标签提供者 viewer.setContentProvider(new MyContentProvider()); viewer.setLabelProvider(new MyLabelProvider()); // 添加一个搜索过滤器 MyFilter filter = new MyFilter(); viewer.addFilter(filter); // 创建一个 Text 控件用于输入搜索关键字 Text searchText = new Text(parent, SWT.BORDER | SWT.SEARCH | SWT.ICON_SEARCH); searchText.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL)); // 为 Text 控件添加 ModifyListener,在用户输入搜索关键字时更新过滤器 searchText.addModifyListener(e -> { filter.setSearchText(searchText.getText()); viewer.refresh(); }); ``` 在上面的代码中,`MyContentProvider` 和 `MyLabelProvider` 分别是内容提供者和标签提供者类,你需要根据自己的需求实现这两个类。`MyFilter` 类是搜索过滤器类,你需要根据自己的需求实现该类。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值