Java JTable Header getColumnHeaderWidth(TableColumn col, JTable table)

Here you can find the source of getColumnHeaderWidth(TableColumn col, JTable table)

Description

Figures out the width of the column header for the given TableColumn.

License

Open Source License

Parameter

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.

Return

int

Declaration

private static int getColumnHeaderWidth(TableColumn col, JTable table) 

Method Source Code

//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;
    }
}

Related

  1. adjustHeader(JTable table)
  2. calcHeaderWidth(int col)
  3. calcHeaderWidth(JTable table, int col)
  4. cleanHeaderCells(JTable table)
  5. createTableHeaderEmptyColumnPainter(final JTable table)
  6. getHeaderDimension(JTable table, TableColumn tableColumn)
  7. getMaxColumnWidth(JTable aTable, int columnNo, boolean includeColumnHeaderWidth, int columnPadding)
  8. getSelectedColumn(JTableHeader tableHeader)
  9. getTableHeaderBackground()