简体   繁体   English

如何在动态更新的JFace TableViewer上添加ViewerFilter?

[英]How to add a ViewerFilter on a JFace TableViewer that update dynamically?

I want to add in a SWT/JFace application a search functionality that filter a TableViewer as the user enter text in the search text field. 我想在SWT / JFace应用程序中添加一个搜索功能,当用户在搜索文本字段中输入文本时,该功能会过滤TableViewer。

final Text filterText = new Text(parent, SWT.NONE);
filterText.addModifyListener(new ModifyListener() {
    @Override
    public void modifyText(ModifyEvent arg0) {
        //TODO how to update the viewer filter with the new text ?
    }
});

TableViewer tableViewer = new TableViewer(...);

ViewerFilter filterViewer = new ViewerFilter() {
    @Override
    public boolean select(Viewer viewer, Object parentElement, Object element) {
        if (filterText.getText() == "") {
            return true;
        }
        //do my stuff to know if element need to be filtered or not
        return false;
    }
};
tableViewer.addFilter(filterViewer);

Do I need to remove the filter and create a new one in the modify listener or is there a better solution? 我是否需要删除过滤器并在修改侦听器中创建一个新过滤器,还是有更好的解决方案?

Basically, you need to have a way of passing the entered text to the filter, in your select method you should filter based on this text, and in your text widget's listener pass the text to the filter and call viewer.refresh() on your table. 基本上,您需要有一种方法将输入的文本传递给过滤器,在您选择的方法中,您应该根据此文本进行过滤,并在文本小部件的监听器中将文本传递给过滤器并调用viewer.refresh()表。

This example should help you: http://www.vogella.com/tutorials/EclipseJFaceTableAdvanced/article.html#jfacetable_filter 这个例子可以帮助你: http//www.vogella.com/tutorials/EclipseJFaceTableAdvanced/article.html#jfacetable_filter

org.eclipse.ui.dialogs.FilteredTree is specifically available for that purpose. org.eclipse.ui.dialogs.FilteredTree专门用于此目的。 Why can't you use that? 你为什么不能用它?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM