Hi all,

it seems that I am stuck with a problem that I just cannot get a grip on.
I want to be able to filter the content that is displayed in a JTable. I
want to be able to attach a filter to each column and a row should only be
displyed when the filters of the columns of that row match.

I have done something similar for sorting the content of the JTable, but
for exclusion filtering it seems to be way more difficult, because there
is always a piece of necessary information missing.

My approach is to subclass the TableModel interface in order to add some
methods that support filtering. The interface looks like this:

interface FilterableTableModel extends TableModel
{
    /**
     * This constant is used to connect the filters of all columns via
     * AND.
     * In this mode all available filters must match for a row to become
     * visible.
     */
    public static final int COLUMN_FILTER_MODE_AND = 0;

    /**
     * This constant is used to connect the filters of all columns via OR.
     * In this mode at least one of the available filters must match for a
     * row to become visible.
     */
    public static final int COLUMN_FILTER_MODE_OR = 1;


    /**
     * Returns the current column filter mode.
     */
    public int getColumnFilterMode();


    /**
     * Returns the Filter for the specified column or null when no filter
     * is specified.
     */
    public Filter getFilterForColumn(int col);


    /**
     * Returns whether or not filtering is enabled in this model.
     */
    public boolean isFilteringSupported();


    /**
     * Determines whether or not a row is visible. To find out if a row is
     * visible the filters of all columns need to be checked and are then
     * connected via the column filter mode (AND/OR). When everything
     * matches according to the mode, the row becomes visible.
     */
    public boolean isRowVisible(int row);


    /**
     * Sets the column filter mode.
     */
    public void setColumnFilterMode(int mode);


    /**
     * Sets the Filter for the specified column.
     */
    public void setFilterForColumn(int col, Filter filter);
}

Now setting the filters is trivial. The problem arises when I want to
implement isRowVisible() (which itself needs to be called by
TableModel.getRowCount()). This method needs to go over all columns that
have filters attached to them, get their cell values and apply the filter.
This means I need to call TableModel.getValueAt() from that method.
TableModel.getValueAt() requires the columnIndex as parameter. I am unable
to determine this parameter. TableModel.getColumnCount() only returns the
number of columns but not the actual indices. Those do not necessarily be
sequential from 0..n.

Maybe my approach is wrong or maybe I am missing something. I would be
glad if someone could point me to a solution.

Greetings,
Ralph

-----------------------------------------------------
 Ralph Kar               |
 Software Developer      | mailto:[EMAIL PROTECTED]
 RTS Realtime Systems AG | http://www.rtsgroup.net
-----------------------------------------------------

_______________________________________________
Advanced-swing mailing list
[EMAIL PROTECTED]
http://eos.dk/mailman/listinfo/advanced-swing

Reply via email to