Java JTable Column filterTable(JTable table, DefaultTableModel dtm, String text, int[] columns, boolean forceRegEx)

Here you can find the source of filterTable(JTable table, DefaultTableModel dtm, String text, int[] columns, boolean forceRegEx)

Description

filter Table

License

Open Source License

Declaration

public static void filterTable(JTable table, DefaultTableModel dtm, String text, int[] columns,
            boolean forceRegEx) 

Method Source Code

//package com.java2s;
/*/* ww w.  ja va2  s  .  c  o m*/
 * Zettelkasten - nach Luhmann
 ** Copyright (C) 2001-2014 by Daniel L?decke (http://www.danielluedecke.de)
 * 
 * Homepage: http://zettelkasten.danielluedecke.de
 * 
 * 
 * This program is free software; you can redistribute it and/or modify it under the terms of the
 * GNU General Public License as published by the Free Software Foundation; either version 3 of 
 * the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with this program;
 * if not, see <http://www.gnu.org/licenses/>.
 * 
 * 
 * Dieses Programm ist freie Software. Sie k?nnen es unter den Bedingungen der GNU
 * General Public License, wie von der Free Software Foundation ver?ffentlicht, weitergeben
 * und/oder modifizieren, entweder gem?? Version 3 der Lizenz oder (wenn Sie m?chten)
 * jeder sp?teren Version.
 * 
 * Die Ver?ffentlichung dieses Programms erfolgt in der Hoffnung, da? es Ihnen von Nutzen sein 
 * wird, aber OHNE IRGENDEINE GARANTIE, sogar ohne die implizite Garantie der MARKTREIFE oder 
 * der VERWENDBARKEIT F?R EINEN BESTIMMTEN ZWECK. Details finden Sie in der 
 * GNU General Public License.
 * 
 * Sie sollten ein Exemplar der GNU General Public License zusammen mit diesem Programm 
 * erhalten haben. Falls nicht, siehe <http://www.gnu.org/licenses/>.
 */

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class Main {
    public static void filterTable(JTable table, DefaultTableModel dtm, String text, int[] columns,
            boolean forceRegEx) {
        if (null == table || null == dtm || null == text || text.isEmpty() || null == columns
                || columns.length < 1) {
            return;
        }
        if (!forceRegEx) {
            text = text.toLowerCase();
        }
        for (int cnt = table.getRowCount() - 1; cnt >= 0; cnt--) {
            int rowindex = table.convertRowIndexToModel(cnt);
            String value = "";
            for (int ci = 0; ci < columns.length; ci++) {
                value = value + dtm.getValueAt(rowindex, columns[ci]).toString().toLowerCase() + " ";
            }
            value = value.trim();
            if (forceRegEx) {
                try {
                    Pattern pattern = Pattern.compile(text);
                    Matcher matcher = pattern.matcher(value);
                    if (!matcher.find()) {
                        dtm.removeRow(rowindex);
                    }
                } catch (PatternSyntaxException ex) {
                    if (!value.contains(text)) {
                        dtm.removeRow(rowindex);
                    }
                }
            } else if (text.contains("?")) {
                try {
                    String dummy = text.replace("?", ".");
                    dummy = dummy.replace("\\.", "\\?").toLowerCase();
                    Pattern pattern = Pattern.compile(dummy);
                    Matcher matcher = pattern.matcher(value);
                    if (!matcher.find()) {
                        dtm.removeRow(rowindex);
                    }
                } catch (PatternSyntaxException ex) {
                    if (!value.contains(text)) {
                        dtm.removeRow(rowindex);
                    }
                }
            } else if (!value.contains(text)) {
                dtm.removeRow(rowindex);
            }
        }
    }
}

Related

  1. convertSelectedColumnsToModelIndices(JTable table, int[] columns)
  2. createColumnNames(TableModel model)
  3. createDefaultTableModel(String[] columnNames)
  4. createUneditableTableModel(String... columnNames)
  5. ensureColumnCount(int count, JTable table)
  6. findColumn(TableModel m, String columnName)
  7. findColumn(TableModel model, String name)
  8. fitColumns(JTable table)
  9. FitTableColumns(JTable myTable)