Wednesday, January 26, 2011

Hiding columns in jTable using swing:


   Hiding column in jTable is not same as hiding a button or any jComponent. Column in jTable is of type TableColumn. TableCoulmn does not extend JComponent and so we cannot hide a column in jTable just by setting visibilty false,i.e., tableColumn.setVisible(false) will not work in case of jTable column. But we can hide a column in jTable by the following two easy way.

Method 1:
    We can set the width of the column to zero to make it visible false. And to make it visible again, we have to set the size again. We need to set the min,max and preferred size.


    To hide the column:
        jTable1.getColumnModel().getColumn(index).setMaxWidth(0);
        jTable1.getColumnModel().getColumn(index).setMinWidth(0);
        jTable1.getColumnModel().getColumn(index).setPreferredWidth(0);
   

   To make the column visible again:
      jTable1.getColumnModel().getColumn(index).setMaxWidth(WIDTH_TO_SET);
      jTable1.getColumnModel().getColumn(index).setMinWidth(WIDTH_TO_SET);
      jTable1.getColumnModel().getColumn(index).setPreferredWidth(WIDTH_TO_SET);
   

index: index of the column to hide.

Replace WIDTH_TO_SET with the size you want.

Method 2:
    TableColumnModel contains all the column information of the jTable. By removing the coulmn from table column model we can make it hidden, and to make it visible we need to add it again. But when we add a column to the TableColumnModel it appends the column to the existing ones. This means the coulmn will be added to the right side of the last coulmn of the table. So after adding column to the tableColumnModel we need to move the column to its previous index to make it look the same as it was before hidding the column. To accomplish this we have save the reference of the removed column and its index before hidding so that when we need to show the same column again we can add the same reference to the tableColumnModel and also move the column to its original position.


    Mehtod to remove column:
        /*save the column if we need to make it visible later.
          You can also make a custom class which will contains the tableColumn   object and also the original index. Maintain a list of the objects of this custom class. This list will contain all the column that are made hidden.
        */
        //to simply hide second column.  
        TableColumn col = jTable1.getColumnModel().getColumn(1);
        int index = 1;
        jTable1.getColumnModel().removeColumn(jTable1.getColumnModel().getColumn(1));   
To make the column visible.
        /* If you have written your own class for saving removed columns then just    get that coulmn object and index and add it accordingly.
        */
        //simply to make second column visible.

    //Get the column object for the list or map maintained for the removed columns.
        TableColumn col = getColumnObejct();
        jTable1.getColumnModel().addColumn(col); //add it to the righmost side.
       //move the column to the index 1.        
    jTable1.getColumnModel().moveColumn(jTable1.getColumnModel().getColumnCount()-1, 1);
  

For free computer engineering JAVA programming books you can visit:
Free Computer Books

4 comments:

  1. Thank's it's a great idea

    ReplyDelete
  2. I prefer the method 1, adding the code for disabling ctrl-c. Thanks.

    ReplyDelete
  3. table.getColumnExt(nuber of column).setVisible(false);

    ReplyDelete