Java JTable Column Width Get getPreferredHeaderWidth(JTable tbl, int iColumn)

Here you can find the source of getPreferredHeaderWidth(JTable tbl, int iColumn)

Description

calculates the preferred header width by asking the header renderer.

License

Open Source License

Parameter

Parameter Description
tbl a parameter
iColumn a parameter

Return

the preferred header width for column iColumn of the given table

Declaration

public static int getPreferredHeaderWidth(JTable tbl, int iColumn) 

Method Source Code


//package com.java2s;
//it under the terms of the GNU Affero General Public License as published by

import java.awt.Component;

import javax.swing.JTable;

import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;

public class Main {
    /**/*from w  ww .jav a  2s. co m*/
     * calculates the preferred header width by asking the header renderer.
     * @param tbl
     * @param iColumn
     * @return the preferred header width for column <code>iColumn</code> of the given table
     */
    public static int getPreferredHeaderWidth(JTable tbl, int iColumn) {
        TableColumn column = tbl.getColumnModel().getColumn(iColumn);

        TableCellRenderer renderer = column.getHeaderRenderer();
        if (renderer == null) {
            renderer = tbl.getTableHeader().getDefaultRenderer();
        }
        final Component compHeader = renderer.getTableCellRendererComponent(tbl, column.getHeaderValue(), false,
                false, 0, iColumn);

        int result = compHeader.getPreferredSize().width;

        result += tbl.getIntercellSpacing().getWidth();
        // this is necessary for JRE 1.4+

        return result;
    }
}

Related

  1. getColumnWidths(final JTable table)
  2. getColumnWidths(JTable p_Table)
  3. getColumnWidths(JTable tbl)
  4. getPreferredColumnWidth(JTable table, int vColIndex, int margin)
  5. getPreferredColumnWidth(JTable table, TableColumn col)