Java JTable Row updateTableSizes(JTable table, int rows)

Here you can find the source of updateTableSizes(JTable table, int rows)

Description

Updates the size of the table rows according to the size of the rendered component.

License

Open Source License

Parameter

Parameter Description
table the table to handle.
rows the maximum rows to be displayed (-1 for unlimited)

Declaration

public static void updateTableSizes(JTable table, int rows) 

Method Source Code

//package com.java2s;
/*/*from w  w w  .  ja va  2s .  co  m*/
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
 * or http://forgerock.org/license/CDDLv1.0.html.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at legal-notices/CDDLv1_0.txt.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information:
 *      Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 *
 *      Copyright 2008-2010 Sun Microsystems, Inc.
 *      Portions Copyright 2011-2015 ForgeRock AS
 */

import java.awt.Component;

import java.awt.Dimension;

import java.awt.Toolkit;

import javax.swing.JTable;

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

public class Main {
    /**
     * Updates the size of the table rows according to the size of the
     * rendered component.
     * @param table the table to handle.
     */
    public static void updateTableSizes(JTable table) {
        updateTableSizes(table, -1);
    }

    /**
     * Updates the size of the table rows according to the size of the
     * rendered component.
     * @param table the table to handle.
     * @param rows the maximum rows to be displayed (-1 for unlimited)
     */
    public static void updateTableSizes(JTable table, int rows) {
        int horizontalMargin = table.getIntercellSpacing().width;
        int verticalMargin = table.getIntercellSpacing().height;
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

        int headerMaxHeight = 5;
        int headerMaxWidth = 0;

        JTableHeader header = table.getTableHeader();
        if (header != null && header.isVisible()) {
            for (int col = 0; col < table.getColumnCount(); col++) {
                TableColumn tcol = table.getColumnModel().getColumn(col);
                TableCellRenderer renderer = tcol.getHeaderRenderer();
                if (renderer == null) {
                    renderer = table.getTableHeader().getDefaultRenderer();
                }
                Component comp = renderer.getTableCellRendererComponent(table, table.getModel().getColumnName(col),
                        false, false, 0, col);
                int colHeight = comp.getPreferredSize().height + 2 * verticalMargin;
                if (colHeight > screenSize.height) {
                    // There are some issues on Mac OS and sometimes the preferred size
                    // is too big.
                    colHeight = 0;
                }
                headerMaxHeight = Math.max(headerMaxHeight, colHeight);
            }
        }

        for (int col = 0; col < table.getColumnCount(); col++) {
            int colMaxWidth = 8;
            TableColumn tcol = table.getColumnModel().getColumn(col);
            TableCellRenderer renderer = tcol.getHeaderRenderer();

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

            if (renderer != null) {
                Component comp = renderer.getTableCellRendererComponent(table, table.getModel().getColumnName(col),
                        false, false, 0, col);
                colMaxWidth = comp.getPreferredSize().width + 2 * horizontalMargin + 8;
            }

            if (colMaxWidth > screenSize.width) {
                colMaxWidth = 8;
            }

            for (int row = 0; row < table.getRowCount(); row++) {
                renderer = table.getCellRenderer(row, col);
                Component comp = table.prepareRenderer(renderer, row, col);
                int colWidth = comp.getPreferredSize().width + 2 * horizontalMargin;
                colMaxWidth = Math.max(colMaxWidth, colWidth);
            }
            tcol.setPreferredWidth(colMaxWidth);
            headerMaxWidth += colMaxWidth;
        }

        if (header != null && header.isVisible()) {
            header.setPreferredSize(new Dimension(headerMaxWidth, headerMaxHeight));
        }

        int maxRow = table.getRowHeight();
        for (int row = 0; row < table.getRowCount(); row++) {
            for (int col = 0; col < table.getColumnCount(); col++) {
                TableCellRenderer renderer = table.getCellRenderer(row, col);
                Component comp = renderer.getTableCellRendererComponent(table,
                        table.getModel().getValueAt(row, col), false, false, row, col);
                int colHeight = comp.getPreferredSize().height + 2 * verticalMargin;
                if (colHeight > screenSize.height) {
                    colHeight = 0;
                }
                maxRow = Math.max(maxRow, colHeight);
            }
        }
        if (maxRow > table.getRowHeight()) {
            table.setRowHeight(maxRow);
        }
        Dimension d1;
        if (rows == -1) {
            d1 = table.getPreferredSize();
        } else {
            d1 = new Dimension(table.getPreferredSize().width, rows * maxRow);
        }
        table.setPreferredScrollableViewportSize(d1);
    }
}

Related

  1. removeTableRows(DefaultTableModel model)
  2. rowToModelIndex(JTable table, int row)
  3. setRowLines(JTable table, int row, int lines)
  4. setTableSize(JTable table, float percentage, int rows)
  5. setToRowBackground(Component c, JTable table, int row)
  6. writeRow(DefaultTableModel table, List data, int row)