2
votes

Is there any way to do it?

4

4 Answers

15
votes

There is the RowFilter<DefaultTableModel, Object> class you can use to filter out rows. The DefaultTableModel can be replaced by your own model. To filter, implement the method

@Override
public boolean include(Entry entry) {
    // All rows are included if no filter is set
    if (filterText.isEmpty())
        return true;

    // If any of the column values contains the filter text,
    // the row can be shown
    for (int i = 0; i < entry.getValueCount(); i++) {
        String value = entry.getStringValue(i);
        if (value.toLowerCase().indexOf(filterText) != -1)
            return true;
    }

    return false;
}

When accessing rows, for instance listening to ListSelectionEvents, do not forget to translate the visible row index to the complete row index in your model. Java provides a function for this as well:

public void valueChanged(ListSelectionEvent e) {
    ListSelectionModel lsm = (ListSelectionModel) e.getSource();

    int visibleRowIndex = ... // Get the index of the selected row

    // Convert from the selection index based on the visible items to the
    // internal index for all elements.
    int internalRowIndex = tableTexts
            .convertRowIndexToModel(visibleRowIndex);

    ...
}
8
votes

Check out Sun's Tutorial for JTables and look at the Sorting and Filtering section.

1
votes

You could set up a arraylists for each column that are populated by values filtered for and implement these in a custom renderer. If a cells entire row values are not met the renderer recursively calls itself with row+1.

if the cells row does meet criteria, it gets rendered, another arraylist stores the row numbers already rendered, its best explained by example this method is in customer renderer extends JLabel

public Component getTableCellRendererComponent(JTable table, Object color,
        boolean isSelected, boolean hasFocus, int row, int column) {

    Object value;
    String s;

    try {
        if (row == 0) {
            drawn[column].clear();
        }// drawn is arraylist which stores cols rend
        if (row > table.getModel().getDataVector.size()) {
            return null;
        }// if we go too far
        if (drawn[column].contains(Integer.toString(row)) == true) {
            // already rendered?
            return getTableCellRendererComponent(table, color, isSelected,
                    hasFocus, (row + 1), column);
        }// render row+1

        for (int i = 0; i < filters.length; i++) {
            value = table.getModel().getValueAt(row, i);
            s = (i == 1) ? df.format(value) : value.toString();
            if (filters[i].contains(s) != true) {
                //try and put in next row, if succeeds when it reaches column 8 it adds row to
                return getTableCellRendererComponent(table, color,
                        isSelected, hasFocus, (row + 1), column);
            }
        }

        value = table.getModel().getValueAt(row, column);

        setFont(getFont().deriveFont(Font.BOLD));

        if ((isSelected == false)) {

            if ((column == 1)) {
                setForeground(Color.magenta);
            }// just formatting
            else {
                setForeground(Color.black);
                setBackground(Color.white);
            }

        } else {
            setBackground(Color.blue);
            setForeground(Color.white);
        }

        if ((column == 1))// col 1 is a date, other columns strings
        {
            setText((value == null) ? "" : df.format(value));
        } else {
            setText((value == null) ? "" : value.toString());
        }

        todayStr = df.format(new java.util.Date());
        dateval = table.getModel().getValueAt(row, 1);
        String datevalStr = df.format(dateval);
        if (datevalStr.equals(todayStr)) {
            setForeground(Color.red);
        }
        drawn[column].add(Integer.toString(row));// mark row as rendered

    } catch (Exception e) {
        e.getMessage();
        return null;
    }
    return this;
}
-3
votes

The best easiest way would be to remove the corresponding element from the model.