Here you can find the source of getWidestCellInColumn(TableColumn col, JTable table)
Parameter | Description |
---|---|
col | The TableColumn object contianing the cells we're basing the size on. |
table | the JTable object that will contiain the given TableColumn. Used to get the renderer object. |
private static int getWidestCellInColumn(TableColumn col, JTable table)
//package com.java2s; import java.awt.*; import javax.swing.*; import javax.swing.table.*; public class Main { /** Module name */ private static final String MODULE_NAME = "FusionUtils."; /**/* w ww . j a v a2 s.co m*/ * Figures out the width of the widest cell in a TableColumn * Lifted from Graphic Java's chapter on Tables. * * @param col The TableColumn object contianing the cells we're basing the size on. * @param table the JTable object that will contiain the given TableColumn. Used to get the renderer object. * * @return int */ private static int getWidestCellInColumn(TableColumn col, JTable table) { String methodName = MODULE_NAME + "getWidestCellInColumn(TableColumn, JTable)"; int retval = -1; int modelIndex = col.getModelIndex(); int width = 0; int maxWidth = 0; for (int i = 0; i < table.getRowCount(); i++) { TableCellRenderer renderer = table.getCellRenderer(i, modelIndex); Component comp = renderer.getTableCellRendererComponent(table, table.getValueAt(i, modelIndex), false, false, i, modelIndex); width = comp.getPreferredSize().width; maxWidth = width > maxWidth ? width : maxWidth; } //JGD Fudge it just a little... retval = maxWidth + 5; //Logger.log( methodName + " retval: " + retval, Logger.INFO ); return retval; } }