Here you can find the source of getPreferredWidthForColumn(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. |
public static int getPreferredWidthForColumn(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."; /**/*from w w w .j a v a2 s . c o m*/ * Takes a TableColumn and determines the preferred width for the entire column based on the contents * of the row cells. 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 */ public static int getPreferredWidthForColumn(TableColumn col, JTable table) { String methodName = MODULE_NAME + "getPreferredWidthForColumn(TableColumn,JTable)"; int retval = 0; int headerWidth = getColumnHeaderWidth(col, table); int cellWidth = getWidestCellInColumn(col, table); retval = headerWidth > cellWidth ? headerWidth : cellWidth; //retval += 20; //JGD Fudge Factor //Logger.log( methodName + " retval: " + retval, Logger.INFO ); return retval; } /** * Figures out the width of the column header for the given TableColumn. * Lifted from Graphic Java's chapter on Tables. * * @param col The TableColumn object contianing the header 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 getColumnHeaderWidth(TableColumn col, JTable table) { String methodName = MODULE_NAME + "getColumnHeaderWidth(TableColumn.JTable)"; int retval = -1; //JGD THis seems to be returning null a lot. Could my friends at Graphic Java be lying to me??? //JGD Yes, they were. The default value of TableColumn.headerRenderer is null. If it's null, it //JGD uses the defaultHeaderRenderer which is just a JLabel (See TableColumn.java) 3/19/03 TableCellRenderer renderer = col.getHeaderRenderer(); if (renderer != null) { Component comp = renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false, false, 0, 0); retval = comp.getPreferredSize().width; } else //renderer is null, the default is a label, create our own label to figure it out { JLabel label = new JLabel((String) col.getHeaderValue(), SwingConstants.CENTER); retval = label.getPreferredSize().width; } //Logger.log( methodName + " retval: " + retval, Logger.INFO ); return retval; } /** * 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; } }