我使用SWT创建了一个列表,其中显示了动物列表:猫,狗,骆驼,大象。现在我需要搜索coloumn中的特定动物前狗,并且只需要在列表中显示动物。我编写了代码来过滤列表,但列表没有过滤,我无法找到问题所在。示例代码如下
final List list = new List(listComposite, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL);
gridData = new GridData();
gridData.horizontalAlignment = SWT.FILL;
gridData.grabExcessHorizontalSpace = true;
gridData.verticalAlignment = SWT.FILL;
gridData.grabExcessVerticalSpace = true;
list.setLayoutData(gridData);
final Map<String,String> descriptionMappernewer = DescriptionParsers.getListOfFXToolMethods();
for(String key: descriptionMappernewer.keySet())
list.add(key);
final MyFilter filter = new MyFilter();
final ListViewer viewer = new ListViewer(listComposite);
//viewer.getList();
viewer.getList().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
//viewer.getList();
viewer.setContentProvider(new ArrayContentProvider());
viewer.setInput(list);
//viewer.setComparer( list);
/*
comparator = new MyViewerComparator();
viewer.setComparator(comparator);
viewer.setSorter(sorter);
*/
txtName.addListener(SWT.Verify, new Listener()
{
@Override
public void handleEvent(Event e)
{
final String oldS = ((Text) e.widget).getText();
final String newS = oldS.substring(0, e.start) + e.text + oldS.substring(e.end);
System.out.println(newS);
//MyFilter filter;
filter.setSearchText(newS);
viewer.refresh();
}
});
viewer.addFilter(filter);
MyFilterClass如下
private static class MyFilter extends ViewerFilter
{
private String searchString;
public void setSearchText(String s)
{
this.searchString = ".*" + s + ".*";
System.out.println(s);
}
@Override
public boolean select(Viewer viewer, Object parentElement, Object element)
{
if (searchString == null || searchString.length() == 0)
{
System.out.println("no string");
return true;
}
String p = (String) element;
if (p.matches(searchString))
{
System.out.println(searchString);
return true;
}
return false;
}
}
请帮助我过滤列表,因为我是jface的新手,无法找到错误
答案 0 :(得分:1)
您正在将org.eclipse.swt.widgets.List
传递给ListViewer.setInput
方法 - 这是错误的。
您在上一个问题中提供的代码使用的是java.util.List
,这是正确的用法。
因此,请将列表更改为How to search for required elements in list using jface
中所示所以引用第一个答案使用:
List<String> input = new ArrayList<>();
input.add("Dodo");
input.add("Unicorn");
input.add("Wyvern");
viewer.setInput(input);