2
votes

I'm using Eclipse Indigo SR1 with JDK 1.7, on Windows 7 Pro. I've written a desktop app, Swing based.

My app includes a JTable; it shows many records of type T, one row per record. The table model points to Vector vect, named vect, containing all data to be shown in the JTable.

The app includes a combo, named sele, showing three values: 0, 1, 2.

When sele = 0, every record of vect has to be visible in the JTable.

When sele = 1, the JTable has to show only vect records having odd row index and all records with even row index mustn't be visible. Viceversa, when sele = 2.

So, here's my question: how can I make a row not visible in the JTable ? I can't use the table model, because it points to vect that contains "all" data.

I tried a table cell renderer, but it seems that you can set the color of a cell, but you can't set it not visible or modify its size.

I've tried another way: if r is the row index, and I want that row to be not visible, I write table.setRowHeight(r,0), but this instruction throws an exception, the height can't be set to zero.

I could solve the problem by splitting the data, dividing vect in two, but I don't like that.

Does anybody have an idea ?

thanx in advance, William

PS: someone told me to create a filtering TableModel that wraps the existing TableModel. The filtering model would be sensitive to the filtering criteria and fire the appropriate methods (TableDataChanged) when the filter was changed. The getRowCount method would return the filtered count. The getValueAt method would map the filtered row to the actual row in the underlying TableModel.

Mah, perhaps it's a good idea, but frankly I'm not able to understand it...

1
The filtering is a good idea, or you could also implement 3 different models.talnicolas
JTable supports filtering out off the box, simply use itkleopatra

1 Answers

4
votes

Use a TableRowSorter - which is:

An implementation of RowSorter that provides sorting and filtering using a TableModel. ..

See How to Use Tables & especially Sorting and Filtering for more info.