1 package org.objectweb.jac.aspects.gui.swing; 2 3 import java.awt.Component ; 4 import javax.swing.event.TableModelEvent ; 5 import javax.swing.table.TableCellRenderer ; 6 import org.apache.log4j.Logger; 7 8 12 public class JTable extends javax.swing.JTable { 13 static Logger logger = Logger.getLogger("gui.swing"); 14 15 18 public void tableChanged(TableModelEvent e) { 19 super.tableChanged(e); 20 setPreferredRowHeights(1); 21 } 22 23 28 public void setPreferredRowHeights(int margin) { 29 for (int row=0; row<getRowCount(); row++) { 30 setPreferredRowHeight(row,margin); 31 } 32 } 33 34 40 public void setPreferredRowHeight(int row, int margin) { 41 int height = getRowHeight(); 43 44 for (int col=0; col<getColumnCount(); col++) { 46 try { 47 TableCellRenderer renderer = getCellRenderer(row,col); 48 if (renderer!=null) { 49 Component 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 e) { 57 logger.error("Failed to compute cell height for ("+row+","+col+")",e); 58 } 59 } 60 setRowHeight(row,height); 61 } 62 63 public Component prepareRenderer(TableCellRenderer renderer, int row, int column) { 64 try { 65 return super.prepareRenderer(renderer,row,column); 66 } catch (Exception e) { 67 logger.error("Caught exception in prepareRenderer("+row+","+column+")",e); 68 return null; 69 } 70 } 71 } 72 | Popular Tags |