The easiest way to do this is by implementing a decorator/filter model for your table model.
The decorator would implement TableModel, but tweak the parameters passed to getValueAt(row,col) based on which rows you wanted to hide.
For example
public class RowFilterModel implements TableModel { ... private boolean[] isRowHidden; private int numVisibleRows; public RowFilterModel(TableModel realModel) { this.realModel = realModel; numVisibleRows = realModel.getRowCount(); isRowHidden = new boolean[numVisibleRows]; } public int getRowCount() { return numVisibleRows; } public int getColumnCount() { return realModel.getColumnCount(); } public void setRowVisible(int row, boolean state) { isRowHidden[row] = !state; if (state) numVisibleRows++; else numVisibleRows--; } public Object getValueAt(int row, int col) { // return row-th non-hidden // there are probably more efficient ways to do this... for(int realRow=0; true; realRow++) { if (!isRowHidden[realRow]) { if (row==0) { return realModel.getValueAt(count,col); } row--; } } } }
There’s obviously more to do to make this work (and the above code could contain a few problems), but this should give the basic idea. In particular, you need to set up the event handling for the above table model to properly report when the realTable changes. You’ll also need to watch for changes in the real model adjust the hidden row array if the real table changes.
You could then use this as follows:
JTable t = ...; RowFilterModel filter = new RowFilterModel(t.getModel()); t.setModel(filter); ... filter.setRowVisible(42, true);
Note that I reversed the logic inside to use “isRowHidden” — this takes advantage of the fact that when we create the array, its values will be false, so by default, all rows are visible. If we had called it “isRowVisible”, we would have needed to walk through and set all elements of the array to true.
I’d recommend you read my article “Advanced Model-View-Controller Techniques” at IBM’s VisualAge Developer Domain for more information on the model filtering concept used here. See http://www7.software.ibm.com/vad.nsf/Data/Document2329.