Java JTable Column Width Set setOptimalColumnWidth(final JTable table, final int col)

Here you can find the source of setOptimalColumnWidth(final JTable table, final int col)

Description

sets the optimal column width for the given column.

License

Open Source License

Parameter

Parameter Description
table the table to work on
col the column index

Declaration

public static void setOptimalColumnWidth(final JTable table,
        final int col) 

Method Source Code

//package com.java2s;
/*//from w  w w  . j a v  a 2 s. 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 maximum number of rows to use for calculation. */
    public final static int MAX_ROWS = 100;
    /** the table to work with. */
    protected JTable m_Table;

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

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

        if ((col >= 0) && (col < table.getColumnModel().getColumnCount())) {
            width = calcColumnWidth(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 column width for all columns.
     */
    public void setOptimalColumnWidth() {
        setOptimalColumnWidth(getJTable());
    }

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

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

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

    /**
     * calcs the optimal column width of the given column.
     *
     * @param col      the column index
     * @return      the optimal width
     */
    public int calcColumnWidth(int col) {
        return calcColumnWidth(getJTable(), col);
    }

    /**
     * Calculates the optimal width for the column of the given table. The
     * calculation is based on the preferred width of the header and cell
     * renderer.
     * <br>
     * Taken from the newsgroup de.comp.lang.java with some modifications.<br>
     * Taken from FOPPS/EnhancedTable - http://fopps.sourceforge.net/<br>
     *
     * @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 calcColumnWidth(JTable table, int col) {
        int result;
        TableModel data;
        int rowCount;
        int row;
        int dec;
        Component c;

        result = calcHeaderWidth(table, col);
        if (result == -1)
            return result;

        data = table.getModel();
        rowCount = data.getRowCount();
        dec = (int) Math.ceil((double) rowCount / (double) MAX_ROWS);
        try {
            for (row = rowCount - 1; row >= 0; row -= dec) {
                c = table.prepareRenderer(table.getCellRenderer(row, col),
                        row, col);
                result = Math.max(result, c.getPreferredSize().width + 10);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }

    /**
     * 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. setColumnWidths(JTable p_Table, int[] p_ColumnWidths)
  2. setColumnWidths(JTable table, Insets insets, boolean setMinimum, boolean setMaximum)
  3. setColumnWidths(JTable table, Insets insets, boolean setMinimum, boolean setMaximum)
  4. setDefaultColumnWidth(JTable table, int column)
  5. setMaxnimumColumnWidths(final JTable table, final int... widths)
  6. SetPreferedColumnWIdth(JTable table, int[] widths)
  7. setPreferredColumnWidths(final JTable table, final float[] columnWeigths, final boolean includeHeader)
  8. setPreferredRowHeights(final JTable table)
  9. setRelativeColumnWidths(JTable table, int... widths)