I have created a list using SWT which displays a list of animals ex:cat,dog,camel,elephant. and now I need to search for a specific animal ex dog in search coloumn andonly that animal has to be displayed in the list. I have written the code to filter the list but the list is not getting filtered and I am not able to find where the problem is. The sample code is as follows
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);
The MyFilterClass is as follows
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;
}
}
Please help me to filter the list as I am new to jface not able to find the error
You are passing an org.eclipse.swt.widgets.List
to the ListViewer.setInput
method - this is wrong.
The code you were given in your previous question uses a java.util.List
which is the correct thing to use here.
So change the list to be as shown in How to search for required elements in list using jface
So to quote from the first answer use:
List<String> input = new ArrayList<>();
input.add("Dodo");
input.add("Unicorn");
input.add("Wyvern");
viewer.setInput(input);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.