Java JTable Header setOptimalHeaderWidth(int col)

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

Description

sets the optimal header width for the given column.

License

Open Source License

Parameter

Parameter Description
col the column index

Declaration

public void setOptimalHeaderWidth(int col) 

Method Source Code

//package com.java2s;
/*//from  w  ww .j a  v a  2s  .c o m
 *   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 3 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, see <http://www.gnu.org/licenses/>.
 */

import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;

public class Main {
    /** the table to work with. */
    protected JTable m_Table;

    /**
     * sets the optimal header width for the given column.
     *
     * @param col      the column index
     */
    public void setOptimalHeaderWidth(int col) {
        setOptimalHeaderWidth(getJTable(), col);
    }

    /**
     * sets the optimal header width for the given column.
     *
     * @param table   the table to work on
     * @param col      the column index
     */
    public static void setOptimalHeaderWidth(final JTable table,
            final int col) {
        final int width;

        if ((col >= 0) && (col < table.getColumnModel().getColumnCount())) {
            width = calcHeaderWidth(table, col);

            if (width >= 0) {
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        JTableHeader header = table.getTableHeader();
                        TableColumn column = table.getColumnModel()
                                .getColumn(col);
                        column.setPreferredWidth(width);
                        table.doLayout();
                        header.repaint();
                    }
                });
            }
        }
    }

    /**
     * sets the optimal header width for all columns.
     */
    public void setOptimalHeaderWidth() {
        setOptimalHeaderWidth(getJTable());
    }

    /**
     * sets the optimal header width for alls column if the given table.
     *
     * @param table   the table to work with
     */
    public static void setOptimalHeaderWidth(JTable table) {
        int i;

        for (i = 0; i < table.getColumnModel().getColumnCount(); i++)
            setOptimalHeaderWidth(table, i);
    }

    /**
     * returns the JTable.
     *
     * @return      the table to work on
     */
    public JTable getJTable() {
        return m_Table;
    }

    /**
     * calcs the optimal header width of the given column.
     *
     * @param col      the column index
     * @return      the optimal width
     */
    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();
        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;
    }
}

Related

  1. makeTableHeadersCentered(final JTable table, final boolean dataColsAlso)
  2. modifyTableHeader(JTableHeader head, Vector columnNames)
  3. paintHeader(Graphics graphics, JTable table, int x, int width)
  4. removeBadChars(JTable table, boolean skipHeaderBoolean)
  5. setHeaderIcon(JTable table, final int column, final ImageIcon icon)
  6. setPressedColumn(JTableHeader tableHeader, int columnModelIndex)
  7. setTableHeaderCellRenderer(TableColumn tableCol)
  8. setupAsRowHeader(final JTable table)
  9. sizeColumnsToFit(JTable table, boolean useHeader, boolean useContent)