Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Searching a JList in Java

I am trying to create a search functionality with a JList in Java. I have a list of contacts in a ListModel that I inserted into a JList.. That displays all the contacts just fine. 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). 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 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..

Any help solving this would be of great assistance. Let me know if I need to post other parts of my code.

like image 382
vt-cwalker Avatar asked Mar 26 '26 16:03

vt-cwalker


1 Answers

Consider using a framework which supports filtering of lists, like f.i. SwingX

Then the basic steps are:

  • implement a RowFilter which filters the Contacts based on the name snippets
  • install a DocumentListener to the 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); 
like image 127
kleopatra Avatar answered Mar 29 '26 14:03

kleopatra



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!