KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > gui > swing > JTable


1 package org.objectweb.jac.aspects.gui.swing;
2
3 import java.awt.Component JavaDoc;
4 import javax.swing.event.TableModelEvent JavaDoc;
5 import javax.swing.table.TableCellRenderer JavaDoc;
6 import org.apache.log4j.Logger;
7
8 /**
9  * A table with variable height rows. The height of a row is the
10  * maximum preferred height of its cells.
11  */

12 public class JTable extends javax.swing.JTable JavaDoc {
13     static Logger logger = Logger.getLogger("gui.swing");
14
15     /**
16      * Recompute row height on tableChanged events
17      */

18     public void tableChanged(TableModelEvent JavaDoc e) {
19         super.tableChanged(e);
20         setPreferredRowHeights(1);
21     }
22
23     /**
24      * Compute preferred height of all rows
25      * @param margin margin in pixels to add at the top and bottom of
26      * each row
27      */

28     public void setPreferredRowHeights(int margin) {
29         for (int row=0; row<getRowCount(); row++) {
30             setPreferredRowHeight(row,margin);
31         }
32     }
33
34     /**
35      * Compute the preferred height of a row
36      * @param row row index
37      * @param margin margin in pixels to add at the top and bottom of
38      * the row
39      */

40     public void setPreferredRowHeight(int row, int margin) {
41         // Get the current default height for all rows
42
int height = getRowHeight();
43       
44         // Determine highest cell in the row
45
for (int col=0; col<getColumnCount(); col++) {
46             try {
47                 TableCellRenderer JavaDoc renderer = getCellRenderer(row,col);
48                 if (renderer!=null) {
49                     Component JavaDoc comp = prepareRenderer(renderer,row,col);
50                     if (comp!=null) {
51                         comp.validate();
52                         int h = comp.getPreferredSize().height + 2*margin;
53                         height = Math.max(height,h);
54                     }
55                 }
56             } catch (Exception JavaDoc e) {
57                 logger.error("Failed to compute cell height for ("+row+","+col+")",e);
58             }
59         }
60         setRowHeight(row,height);
61     }
62
63     public Component JavaDoc prepareRenderer(TableCellRenderer JavaDoc renderer, int row, int column) {
64         try {
65             return super.prepareRenderer(renderer,row,column);
66         } catch (Exception JavaDoc e) {
67             logger.error("Caught exception in prepareRenderer("+row+","+column+")",e);
68             return null;
69         }
70     }
71 }
72
Popular Tags