简体   繁体   English

如何在swt jface中使用listviewer过滤列表中所需元素的列表

[英]How to filter a list of required elements in list using listviewer in swt jface

I have created a list using SWT which displays a list of animals ex:cat,dog,camel,elephant. 我使用SWT创建了一个列表,其中显示了动物列表,例如:猫,狗,骆驼,大象。 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 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;
        }
    }    

Please help me to filter the list as I am new to jface not able to find the error 请帮我过滤列表,因为我是jface的新手,无法找到错误

You are passing an org.eclipse.swt.widgets.List to the ListViewer.setInput method - this is wrong. 您正在将org.eclipse.swt.widgets.List传递给ListViewer.setInput方法-这是错误的。

The code you were given in your previous question uses a java.util.List which is the correct thing to use here. 您在上一个问题中给出的代码使用java.util.List ,这是在此处使用的正确方法。

So change the list to be as shown in How to search for required elements in list using jface 因此,将列表更改为如何使用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);

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

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