Java JTable Header calcHeaderWidth(int col)

Here you can find the source of calcHeaderWidth(int col)

Description

calcs the optimal header width of the given column

License

Open Source License

Declaration

public int calcHeaderWidth(int col) 

Method Source Code

//package com.java2s;
/*//from  w  ww.j  a  v a2 s  . c  om
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

import java.awt.Component;

import javax.swing.JTable;

import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;

public class Main {
    private JTable jtable;

    /**
     * calcs the optimal header width of the given column
     */
    public int calcHeaderWidth(int col) {
        return calcHeaderWidth(getJTable(), col);
    }

    /**
     *  Calculates the optimal width for the header of the given table. The
     *  calculation is based on the preferred width of the header renderer.
     *
     *  @param table    the table to calculate the column width
     *  @param col      the column to calculate the widths
     *  @return         the width, -1 if error
     */
    public static int calcHeaderWidth(JTable table, int col) {
        if (table == null)
            return -1;

        if (col < 0 || col > table.getColumnCount()) {
            System.out.println("invalid col " + col);
            return -1;
        }

        JTableHeader header = table.getTableHeader();
        TableCellRenderer defaultHeaderRenderer = null;
        if (header != null)
            defaultHeaderRenderer = header.getDefaultRenderer();
        TableColumnModel columns = table.getColumnModel();
        TableModel data = table.getModel();
        TableColumn column = columns.getColumn(col);
        int width = -1;
        TableCellRenderer h = column.getHeaderRenderer();
        if (h == null)
            h = defaultHeaderRenderer;
        if (h != null) {
            // Not explicitly impossible
            Component c = h.getTableCellRendererComponent(table,
                    column.getHeaderValue(), false, false, -1, col);
            width = c.getPreferredSize().width + 5;
        }

        return width;
    }

    /**
     * returns the JTable
     */
    public JTable getJTable() {
        return jtable;
    }
}

Related

  1. AddToTable(JTable tbl, Object[] obj, boolean head)
  2. adjustHeader(JTable table)
  3. calcHeaderWidth(JTable table, int col)
  4. cleanHeaderCells(JTable table)
  5. createTableHeaderEmptyColumnPainter(final JTable table)
  6. getColumnHeaderWidth(TableColumn col, JTable table)