Filtrer une JTable

Author:


{filelink=902}


import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.RowFilter;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

public class FiltrageJTable {
  public static void main(String[] args) {
	String nomColonne[] = { "Mois", "Valeur" };
    Object[][] valeur = { { "Janvier", 0 }, { "Mares", 2 }, { "Ocrobre", 9 }, { "Decembre", 11 } };
    TableModel model = new DefaultTableModel(valeur, nomColonne) {
      public Class getColumnClass(int column) {
        return getValueAt(0, column).getClass();
      }
    };
    JTable table = new JTable(model);

    RowFilter filter = new RowFilter() {
      public boolean include(Entry entry) {
        Integer population = (Integer) entry.getValue(1);
        return population.intValue() > 3;
      }
    };

    TableRowSorter sorter = new TableRowSorter(model);
    sorter.setRowFilter(filter);
    table.setRowSorter(sorter);
    JScrollPane scrollPane = new JScrollPane(table);
    JFrame frame = new JFrame("Filtrage de JTable");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(scrollPane);
    frame.setSize(300, 200);
    frame.setVisible(true);
  }
}

Leave a Reply

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