How can I hide (not remove) a row in a JTable ??

Scott Stanchfield

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)

  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);

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());


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

0 Comments  (click to add your comment)
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



About | Sitemap | Contact