Java JTable Row Height packRowHeights(final JTable table)

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

Description

Packs all table rows to their preferred height.

License

Open Source License

Parameter

Parameter Description
table table to process

Declaration

public static void packRowHeights(final JTable table) 

Method Source Code

//package com.java2s;
/*//w ww  . j  a  v  a  2s  .c  o m
 * This file is part of WebLookAndFeel library.
 *
 * WebLookAndFeel library 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.
 *
 * WebLookAndFeel library 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 WebLookAndFeel library.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.awt.Component;

import java.awt.Dimension;

import java.awt.Insets;

import javax.swing.JTable;

import javax.swing.table.TableCellRenderer;

public class Main {
    /**
     * Packs all table rows to their preferred height.
     *
     * @param table
     *            table to process
     */
    public static void packRowHeights(final JTable table) {
        for (int row = 0; row < table.getRowCount(); row++) {
            int maxHeight = 0;
            for (int column = 0; column < table.getColumnCount(); column++) {
                final TableCellRenderer cellRenderer = table.getCellRenderer(row, column);
                final Object valueAt = table.getValueAt(row, column);
                final Component renderer = cellRenderer.getTableCellRendererComponent(table, valueAt, false, false,
                        row, column);
                final int heightPreferable = renderer != null ? renderer.getPreferredSize().height : 0;
                maxHeight = Math.max(heightPreferable, maxHeight);
            }
            table.setRowHeight(row, maxHeight);
        }
    }

    /**
     * Returns maximum insets combined from the specified ones.
     *
     * @param insets1
     *            first insets
     * @param insets2
     *            second insets
     * @return maximum insets
     */
    public static Insets max(final Insets insets1, final Insets insets2) {
        if (insets1 != null && insets2 != null) {
            return new Insets(Math.max(insets1.top, insets2.top), Math.max(insets1.left, insets2.left),
                    Math.max(insets1.bottom, insets2.bottom), Math.max(insets1.right, insets2.right));
        } else if (insets1 != null) {
            return insets1;
        } else if (insets2 != null) {
            return insets2;
        } else {
            return null;
        }
    }

    /**
     * Returns maximum dimension combined from specified components dimensions.
     *
     * @param component1
     *            first component
     * @param component2
     *            second component
     * @return maximum dimension
     */
    public static Dimension max(final Component component1, final Component component2) {
        return max(component1.getPreferredSize(), component2.getPreferredSize());
    }

    /**
     * Returns maximum dimension combined from specified components dimensions.
     *
     * @param components
     *            components
     * @return maximum dimension
     */
    public static Dimension max(final Component... components) {
        Dimension max = components.length > 0 ? components[0].getPreferredSize() : new Dimension(0, 0);
        for (int i = 1; i < components.length; i++) {
            max = max(max, components[i].getPreferredSize());
        }
        return max;
    }

    /**
     * Returns maximum dimension combined from specified ones.
     *
     * @param dimension1
     *            first dimension
     * @param dimension2
     *            second dimension
     * @return maximum dimension
     */
    public static Dimension max(final Dimension dimension1, final Dimension dimension2) {
        if (dimension1 == null && dimension2 == null) {
            return null;
        } else if (dimension1 == null) {
            return dimension2;
        } else if (dimension2 == null) {
            return dimension1;
        } else {
            return new Dimension(Math.max(dimension1.width, dimension2.width),
                    Math.max(dimension1.height, dimension2.height));
        }
    }
}

Related

  1. correctRowHeight(JTable table)
  2. getPreferredRowHeight(JTable table, int rowIndex, int margin)
  3. getRowBounds(JTable table, int row, int height)
  4. platformTableRowHeight(JTable jtab, boolean forEdit)
  5. setRowHeight(JTable table, int row)
  6. updateRowHeights(JTable table)