Java JTable Column Size Calculate calcColumnWidths(final JTable table)

Here you can find the source of calcColumnWidths(final JTable table)

Description

Calculates and sets the each column to it preferred size.

License

Open Source License

Parameter

Parameter Description
table the table to fix up

Declaration

public static void calcColumnWidths(final JTable table) 

Method Source Code

//package com.java2s;
/* Copyright (C) 2015, University of Kansas Center for Research
 * /* www. j av a  2  s .  co  m*/
 * Specify Software Project, specify@ku.edu, Biodiversity Institute,
 * 1345 Jayhawk Boulevard, Lawrence, Kansas, 66045, USA
 * 
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

import java.awt.Component;

import java.awt.Dimension;

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 {
    /**
     * Calculates and sets the each column to it preferred size.  NOTE: This
     * method also sets the table height to 10 rows.
     * 
     * @param table the table to fix up
     */
    public static void calcColumnWidths(final JTable table) {
        calcColumnWidths(table, 10);
    }

    /**
     * Calculates and sets the each column to it preferred size.  NOTE: This
     * method also sets the table height to 10 rows.
     * 
     * @param table the table to fix up
     * @param numRowsHeight the number of rows to make the table height (or null not to set it)
     */
    public static void calcColumnWidths(final JTable table, final Integer numRowsHeight) {
        calcColumnWidths(table, numRowsHeight, null);
    }

    /**
     * Calculates and sets the each column to it preferred size.  NOTE: This
     * method also sets the table height to 10 rows.
     * 
     * @param table the table to fix up
     * @param numRowsHeight the number of rows to make the table height (or null not to set it)
     */
    public static void calcColumnWidths(final JTable table, final Integer numRowsHeight, final Integer maxWidth) {
        if (table != null) {
            JTableHeader header = table.getTableHeader();

            TableCellRenderer defaultHeaderRenderer = null;

            if (header != null) {
                defaultHeaderRenderer = header.getDefaultRenderer();
            }

            TableColumnModel columns = table.getColumnModel();
            TableModel data = table.getModel();

            int margin = columns.getColumnMargin(); // only JDK1.3

            int rowCount = data.getRowCount();

            int totalWidth = 0;

            for (int i = columns.getColumnCount() - 1; i >= 0; --i) {
                TableColumn column = columns.getColumn(i);

                int columnIndex = column.getModelIndex();

                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,
                            i);

                    width = c.getPreferredSize().width;
                }

                for (int row = rowCount - 1; row >= 0; --row) {
                    TableCellRenderer r = table.getCellRenderer(row, i);

                    Component c = r.getTableCellRendererComponent(table, data.getValueAt(row, columnIndex), false,
                            false, row, i);

                    width = Math.max(width, c.getPreferredSize().width + 10); // adding an arbitray 10 pixels to make it look nicer

                    if (maxWidth != null) {
                        width = Math.min(width, maxWidth);
                    }
                }

                if (width >= 0) {
                    column.setPreferredWidth(width + margin); // <1.3: without margin
                } else {
                    // ???
                }

                totalWidth += column.getPreferredWidth();
            }

            // If you like; This does not make sense for two many columns!
            Dimension size = table.getPreferredScrollableViewportSize();
            //if (totalWidth > size.width)
            {
                if (numRowsHeight != null) {
                    size.height = Math.min(size.height, table.getRowHeight() * numRowsHeight);
                }
                size.width = totalWidth;
                table.setPreferredScrollableViewportSize(size);
            }
        }
    }
}

Related

  1. adjustColumnSizes(JTable table, int column, int margin)
  2. autoSizeCols(JTable table)
  3. autoSizeTableColumns(JTable table)
  4. calcColumnWidth(JTable table, int col)
  5. calcColumnWidths(final JTable table)
  6. calcColumnWidths(JTable table)
  7. calcColumnWidths(JTable table)
  8. calculateColumnWidth(JTable table, int columnIndex)
  9. calculateColumnWidth(JTable table, int columnIndex)