我使用表查看器创建了一个表,现在我需要根据文本框中输入的文本进行过滤,这样我们如何过滤表创建表的代码如下
TableViewerColumn message = new TableViewerColumn(viewer, SWT.NONE);
message.getColumn().setWidth(800);
message.getColumn().setText("Message");
message.setLabelProvider(new ColumnLabelProvider()
{
@Override
public void update(ViewerCell cell)
{
Object element = cell.getElement();
if(element instanceof MyObject)
{
MyObject obj = (MyObject) element;
cell.setText(obj.getMessage());
}
}
});
}
private static class MyObject
{
private String first;
private String second;
private String message;
public MyObject(String first, String second,String message)
{
this.first = first;
this.second = second;
this.message = message;
}
public String getFirst()
{
return first;
}
public void setFirst(String first)
{
this.first = first;
}
public String getSecond()
{
return second;
}
public void setSecond(String message)
{
this.second = second;
}
public String getMessage()
{
return message;
}
public void setMessage(String message)
{
this.message = message;
}
所以现在我们如何过滤表格。请帮助我,因为我是jface table viewer的新手
答案 0 :(得分:2)
使用从ViewerFilter
派生的类添加过滤器:
class MyFilter extends ViewerFilter
{
@Override
public boolean select(Viewer viewer, Object parentElement, Object element)
{
MyObject obj = (MyObject)element;
// TODO return true to include the object, false to exclude
}
}
使用以下内容将其添加到表中:
viewer.addFilter(new MyFilter());
呼叫
viewer.refresh();
让观众在文字发生变化时重新运行过滤器。