swt tableviewer通过列的值筛选行,将选中的行选中
时间: 2024-02-24 11:58:21 浏览: 31
如果您想要在筛选后将选中的行保留选中状态,可以在筛选器中使用 `TableViewer` 的 `setSelection()` 方法将选中的行重新选中。以下是一个示例:
```java
tableViewer.addFilter(new ViewerFilter() {
@Override
public boolean select(Viewer viewer, Object parentElement, Object element) {
// 根据列的值来筛选行
String searchText = "John";
boolean isSelected = tableViewer.getSelection().contains(element); // 判断是否选中
boolean isMatched = ((Person) element).getName().contains(searchText); // 判断是否匹配筛选条件
return isMatched && isSelected;
}
});
tableViewer.addSelectionChangedListener(new ISelectionChangedListener() {
@Override
public void selectionChanged(SelectionChangedEvent event) {
// 保存当前选中行的状态
IStructuredSelection selection = (IStructuredSelection) event.getSelection();
tableViewer.setSelection(selection);
}
});
```
在上面的示例中,我们在筛选器中使用 `isSelected` 变量来判断当前行是否选中,`isMatched` 变量来判断当前行是否匹配筛选条件。如果当前行既选中又匹配,则返回 `true`,否则返回 `false`。然后,我们将选中的行重新选中,并使用 `addSelectionChangedListener()` 方法添加一个选择更改侦听器,以便在选择更改时保存当前选中行的状态。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)