简体   繁体   English

在Java中搜索JList

[英]Searching a JList in Java

I am trying to create a search functionality with a JList in Java. 我正在尝试使用Java中的JList创建搜索功能。 I have a list of contacts in a ListModel that I inserted into a JList.. That displays all the contacts just fine. 我在ListModel中有一个联系人列表,我插入到JList ..这显示所有联系人就好了。 However, I have a search box above the list of contacts and want the contacts to be narrowed down to what the user types in the search box as they type(like Google search). 但是,我在联系人列表上方有一个搜索框,并希望将联系人缩小到用户在键入时在搜索框中输入的内容(如Google搜索)。 However, when i try to type in search box, all contacts disappear and then Im not able to backspace either. 但是,当我尝试键入搜索框时,所有联系人都会消失,然后我也无法退格。 My KeyListener code is as follows: 我的KeyListener代码如下:

KeyListener klisten = new KeyListener() 
    {
        public void keyPressed(KeyEvent evt) 
        {
            searchResults = new ContactList();
            listModel.removeAllElements();
            searchResults.addContact(contactList.getContact(evt.getKeyChar()));
            for (int i = 0; i < searchResults.getContacts().size(); i++)
            {
                listModel.addElement(searchResults.getContact(i).getFname() + " " + searchResults.getContact(i).getLname());
            }
            contacts = new JList(listModel);
            contacts.validate();
        }
        public void keyReleased(KeyEvent evt) {} 
        public void keyTyped(KeyEvent evt) {}
    };
    searchField.addKeyListener(klisten);

EDIT** the original ListModel that contains all original contacts is declared before this anonymous class and is called listModel which I reused in this class to replace full contact list.. 编辑**包含所有原始联系人的原始ListModel在此匿名类之前声明,并被称为listModel,我在此类中重用它来替换完整的联系人列表。

Any help solving this would be of great assistance. 任何帮助解决这个问题都会有很大的帮助。 Let me know if I need to post other parts of my code. 如果我需要发布代码的其他部分,请告诉我。

It's hard to see the logic without the rest of the code. 没有剩下的代码就很难看到逻辑。 Consider posting an SSCCE . 考虑发布SSCCE It looks like you're recreating the JList in the key listener event: 看起来您正在JList密钥监听器事件中重新创建JList

contacts = new JList(listModel);

That new list needs to be added/readded to the container. 需要将新列表添加/读入容器。 Looks like list recreation is not needed as listModel is up to date and it should notify the list to refresh the changes, unless searchResults is empty. 看起来不需要列表重新创建,因为listModel是最新的,它应该通知列表刷新更改,除非searchResults为空。 It is just a speculation without looking at code. 这只是一个没有看代码的猜测。

It may be easier to use a single column JTable with filtering support. 使用具有过滤支持的单列JTable可能更容易。 See Sorting and Filtering for details. 有关详细信息,请参阅排序和筛选

Consider using a framework which supports filtering of lists, like fi SwingX 考虑使用支持过滤列表的框架,例如fi SwingX

Then the basic steps are: 然后基本步骤是:

  • implement a RowFilter which filters the Contacts based on the name snippets 实现一个RowFilter,它根据名称片段过滤联系人
  • install a DocumentListener to the textField 将DocumentListener安装到textField
  • on change notification from the document, install a new filter on the list 在从文档更改通知时,在列表上安装新过滤器

Pseudo-code snippet 伪代码片段

// the custom RowFilter
public class ContactRowFilter extends RowFilter {
    private String compare;

    public ContactRowFilter(String compare) {
        this.compare = compare;
    }

    public boolean include(Entry entry) {
        Contact contact = (Contact) entry.getValue(0);
        return contact.getName().contains(compare);
    }
}

// custom documentListener
public class SearchFieldListener implements DocumentListener {
    private JXList list;

    public SearchFieldListener(JXList list) {
        this.list = list;
    }

    @Override
    public void insertUpdate(...) {
        updateFilter(evt.getDocument());
    }
    ....
    protected void updateFilter(Document doc) {
        String text = doc.getText(0, doc.getLength());
        list.setRowFilter(text.length > 0 ?
            new ContactRowFilter(text) : null);
    }

}

// usage
JXList list = new JXList(myModel);
list.setAutoCreateRowSorter(true);
DocumentListener listener = new SearchFieldListener(list);
JTextField searchField = new JTextField(20);
searchField.getDocument().addDocumentListener(listener); 

I've had a quick read and to be honest, there isn't much to go. 我有一个快速阅读,说实话,没有太多可去的。

I'm not sure of the result of this method 我不确定这种方法的结果

searchResults.addContact(contactList.getContact(evt.getKeyChar()));

And this to me suggests that there are no contacts available 这对我来说表明没有可用的联系人

searchResults = new ContactList();

But that's because I'm missing context. 但那是因为我错过了背景。

A better solution might be to use a "Proxy" model, basically a model that wraps a model, that provides the filter functionality for you or as Max has suggested, a JTable 一个更好的解决方案可能是使用“代理”模型,基本上是一个包装模型的模型,它为您提供过滤器功能,或者正如Max建议的那样,一个JTable

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

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