Identity Table 2

To provide a header for the rowheader, mirror what the table does when it is inserted into a scroll pane. That is, get the table header, and insert in into the scrollpane above the table. Because the table header is built by the JTable, it will always match the look and feel, and look just like the other header elements.

  1 
  2 package com.vizitsolutions.identitytable;
  3 
  4 import javax.swing.table.DefaultTableColumnModel;
  5 import javax.swing.JScrollPane;
  6 import javax.swing.JTable;
  7 import javax.swing.table.TableColumn;
  8 import javax.swing.table.TableColumnModel;
  9 
 10 /**
 11  * <p>$Id$</p>
 12  *
 13  * <p>
 14  * This class maps a TableModel into two JTables, and places
 15  * them into a JScrollPane. The first JTable contains n fixed
 16  * columns, and represents what is commonly called a rowheader.
 17  * This is placed in the rowheader area of the JScrollPane. These
 18  * columns will not scroll horizontally. This is particularly
 19  * useful when you have one or more columns that identify an object
 20  * and several additional columns that contain attributes of the
 21  * object. Placing the identifying columns in these fixed columns
 22  * ensures that they are always visible while the attributes of the
 23  * object are scrolled into view.
 24  * </p>
 25  * <p>
 26  * The second JTable contains the remaining columns and is placed in
 27  * the main viewport and will scroll normally.
 28  * </p>
 29  *
 30  * @author Alex Kluge
 31  * @version $Revision$, $Date$
 32  */
 33 public class IdentityTable2 extends JScrollPane
 34 {
 35     private JTable           mainTable;
 36     private TableColumnModel mainColumnModel      = new DefaultTableColumnModel();
 37     private JTable           rowHeaderTable;
 38     private TableColumnModel rowHeaderColumnModel = new DefaultTableColumnModel();
 39     
 40     /**
 41      * Creates a new instance of IdentityTable1
 42      */
 43     public IdentityTable2(IdentityTableModel baseTableModel)
 44     {
 45         // Start out life as an empty scrollpane.
 46         super();
 47         
 48         // Loop over each of the fixed columns, and create columns in the row
 49         // header column model for them.
 50         TableColumn column;
 51         int         columnCount         = baseTableModel.getColumnCount();
 52         int         currentColumn;
 53         int         identityColumnCount = baseTableModel.getIdentityColumnCount();
 54         
 55         for (currentColumn = 0; currentColumn<identityColumnCount; currentColumn++)
 56         {
 57             column = new TableColumn(currentColumn);
 58             column.setHeaderValue(baseTableModel.getColumnName(currentColumn));
 59             
 60             rowHeaderColumnModel.addColumn(column);
 61         }
 62         
 63         // Now loop over the remaining columns, and add them to the main table's column model.
 64         for (currentColumn = identityColumnCount; currentColumn<columnCount; currentColumn++)
 65         {
 66             column = new TableColumn(currentColumn);
 67             column.setHeaderValue(baseTableModel.getColumnName(currentColumn));
 68             
 69             mainColumnModel.addColumn(column);
 70         }
 71         
 72         // Providing a column model here automatically sets the
 73         // autoCreateColumnsFromModel flag to false.
 74         rowHeaderTable = new JTable(baseTableModel, rowHeaderColumnModel);
 75         rowHeaderTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
 76 
 77         mainTable      = new JTable(baseTableModel, mainColumnModel);
 78         mainTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
 79         
 80         // Ensure that both tables have common selections.
 81         rowHeaderTable.setSelectionModel(mainTable.getSelectionModel());
 82         rowHeaderTable.setPreferredScrollableViewportSize(rowHeaderTable.getPreferredSize());
 83         
 84         // This puts the column headers in the upper left corner above the row
 85         // header columns. This is the same thing that happens when a JTable configures
 86         // the containing JScrollPane.
 87         setCorner(JScrollPane.UPPER_LEFT_CORNER, rowHeaderTable.getTableHeader());
 88 
 89         
 90         setViewportView(mainTable);
 91         setRowHeaderView(rowHeaderTable);
 92     }
 93     
 94     public TableColumnModel getMainColumnModel()
 95     {
 96         return mainColumnModel;
 97     }
 98     
 99     public TableColumnModel getRowHeaderColumnModel()
100     {
101         return rowHeaderColumnModel;
102     }
103 }
104 
105 

Notes