The RowHeaderResizeListener

It is reasonably easy to use a component listener to listen for the resizing (dragging) of the row header table. When the row header's column header is dragged, resize the row header view, layout the main table, then revalidate the JScrollPane. The JScrollPane is the container for both the rowheader and the main table.

 1 
 2 package com.vizitsolutions.identitytable;
 3 
 4 import java.awt.event.ComponentAdapter;
 5 import java.awt.event.ComponentEvent;
 6 import java.awt.Dimension;
 7 import javax.swing.JScrollPane;
 8 import javax.swing.JTable;
 9 import javax.swing.JViewport;
10 
11 /**
12  * <p>$Id: $</p>
13  * <p>
14  * Listen for resizes of the row header table's header, then resize the
15  * containing viewport. These resize events happen when the user drags the
16  * row header's column header to resize the row header columns.
17  * </p>
18  * @author  Alex Kluge
19  * @version $Revision: $, $Date: $
20  */
21 public class RowHeaderResizeListener extends ComponentAdapter
22 {
23     private JScrollPane  identityTable;
24     private JTable       mainTable;
25     private JTable       rowHeaderTable;
26     private JViewport    rowHeaderView;
27     
28     /**
29      * <p>
30      * Create a RowHeaderResizeListener, which listens for resize events on the
31      * row header part of an IdentityTable. These happen when the user drags to
32      * resize the row header column.
33      * </p>
34      * @param identityTable    A JScrollPane, which has been configured as an
35      *                         IdentityTable, with a rowheader table, and a
36      *                         main table.
37      * 
38      * @param rowHeaderView    A JViewport containing the rowheader. The viewport
39      *                         is the component through only a section of the
40      *                         rowheader is visible.
41      *                      
42      * @param rowHeaderTable   A JTable containing the row header columns.
43      * @param mainTable        A JTable containing the main data columns. This
44      *                         table must be resized whenever the rowHeaderTable
45      *                         resizes.
46      * 
47      * @see com.vizitsolutions.identitytable.IdentityTable
48      */
49     public RowHeaderResizeListener(JScrollPane identityTable,  JViewport rowHeaderView,
50                                    JTable      rowHeaderTable, JTable    mainTable)
51     {
52         this.identityTable  = identityTable;
53         this.rowHeaderView  = rowHeaderView;
54         this.rowHeaderTable = rowHeaderTable;
55         this.mainTable      = mainTable;
56     }
57         
58     /**
59      * Called when the rowHeaderTable header is resized. The rowHeaderView
60      * is resized to the appropriate size for the rowHeaderTable, then layout
61      * the main table and finally, call revalidate to trigger a layout of the
62      * containing IdentityTable (JScrollPane).
63      * 
64      * @param e A ComponentEvent, which is ignored because we are only listening
65      *          on the rowheader.
66      */
67     public void componentResized(ComponentEvent e)
68     {
69         Dimension size = rowHeaderView.getPreferredSize();
70         size.width = rowHeaderTable.getWidth();
71         rowHeaderView.setPreferredSize(size);
72         mainTable.doLayout();
73         identityTable.revalidate();
74     }
75 }
76 
77 
78 

Notes