IdentityTable

Make use of the identityColumnCount to build two different tables. The first table is built from the first identityColumnCount columns, and will be placed in the rowheader. A table built from the remaining columns will be placed in the center of the scrollpane. Both of these tables will use the same underlying table model, and the column model will be used to display different columns of data in each table.

 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 IdentityTable1 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 IdentityTable1(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         setViewportView(mainTable);
85         setRowHeaderView(rowHeaderTable);
86     }
87     
88     public TableColumnModel getMainColumnModel()
89     {
90         return mainColumnModel;
91     }
92     
93     public TableColumnModel getRowHeaderColumnModel()
94     {
95         return rowHeaderColumnModel;
96     }
97 }
98 
99 

Notes