Exemple de Filtrage d’une JListe

Author:

Ce Code permet de filtrer un composant JList. Le code ignore la casse


import javax.swing.*;
import java.awt.*;
import java.util.*;
import javax.swing.event.*;
/**
 *
 * @author Administrateur
 */
public class FilteredJList extends JList {

    private FilterField filterField;
    private int DEFAULT_FIELD_WIDTH = 20;

    public FilteredJList() {
        super();
        setModel (new FilterModel());
        filterField = new FilterField (DEFAULT_FIELD_WIDTH);

    }

    public void setModel (ListModel m) {
        if (! (m instanceof FilterModel))
            throw new IllegalArgumentException();
        super.setModel (m);
    }

    public void addItem (Object o) {
        ((FilterModel)getModel()).addElement (o);
    }

    public JTextField getFilterField() {
        return filterField;
    }

class FilterModel extends AbstractListModel {
            ArrayList items;
            ArrayList filterItems;
            public FilterModel() {
               super();
               items = new ArrayList();
               filterItems = new ArrayList();
            }
            public Object getElementAt (int index) {
               if (index < filterItems.size())
                   return filterItems.get (index);
               else
                  return null;
            }
            public int getSize() {
                return filterItems.size();
            }
			public void addElement (Object o) {
             items.add (o);
             refilter();

         }
       private void refilter() {
           filterItems.clear();
           String term = getFilterField().getText().toLowerCase();
           for (int i=0; i
    

Leave a Reply

Your email address will not be published. Required fields are marked *