How to add a ViewerFilter on a JFace TableViewer that update dynamically?
Asked Answered
T

2

6

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.

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?

Terraqueous answered 23/5, 2011 at 9:20 Comment(0)
L
4

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.

This example should help you: http://www.vogella.com/tutorials/EclipseJFaceTableAdvanced/article.html#jfacetable_filter

Lenette answered 23/5, 2011 at 14:24 Comment(0)
S
0

org.eclipse.ui.dialogs.FilteredTree is specifically available for that purpose. Why can't you use that?

Slivovitz answered 23/5, 2011 at 9:48 Comment(3)
I work with tableviewer and not a treeviewer but I can work around to use the same principle. But the big problem is that I am not in an RCP application so I can't use WorkbenchJob.Terraqueous
Why is that you can't use WorkbenchJob in RCP? Its an API and can be used in RCP apps as well.Slivovitz
The problem is : I am NOT in RCP !Terraqueous

© 2022 - 2024 — McMap. All rights reserved.