Here you can find the source of getColumnHeaderWidth(TableColumn col, JTable table)
Parameter | Description |
---|---|
col | The TableColumn object contianing the header 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 getColumnHeaderWidth(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 ww w .j a v a2s .com*/ * 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; } }