Java JTable Column Width Set setPreferredRowHeights(final JTable table)

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

Description

This method will iterate over the given table's rows and attempt to find the optimal height for each.

License

Apache License

Parameter

Parameter Description
table Table to adapt, non-null.

Declaration

public static void setPreferredRowHeights(final JTable table) 

Method Source Code

//package com.java2s;
/**/*from w  w  w .j  av a  2s. c  o  m*/
 * This class holds utility methods which may be useful when dealing with JTables.
 * <p/>
 * <hr/> Copyright 2006-2012 Torsten Heup
 * <p/>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */

import javax.swing.*;

import java.awt.*;

public class Main {
    /**
     * This method will iterate over the given table's rows and attempt to find the optimal height for each. You might
     * want to invoke this method whenever the table's model has been updated or the column margin's change. You can
     * obtain a utility class which does just that for you using the RowHeightAdapter class.
     *
     * @param table Table to adapt, non-null.
     */
    public static void setPreferredRowHeights(final JTable table) {
        if (table == null)
            throw new IllegalArgumentException("Parameter 'table' must not be null!");

        final int rowCount = table.getRowCount();
        final int columnCount = table.getColumnCount();

        for (int row = 0; row < rowCount; row++) {
            int max = 0;
            for (int col = 0; col < columnCount; col++) {
                final Component renderer = table.getCellRenderer(row, col).getTableCellRendererComponent(table,
                        table.getValueAt(row, col), table.isCellSelected(row, col), table.hasFocus(), row, col);
                final Rectangle rect = table.getCellRect(row, col, true);
                renderer.setSize(rect.width, rect.height);
                final int height = renderer.getPreferredSize().height;
                if (height > max)
                    max = height;
            }
            table.setRowHeight(row, max <= 0 ? table.getRowHeight() : max);
        }
    }
}

Related

  1. setDefaultColumnWidth(JTable table, int column)
  2. setMaxnimumColumnWidths(final JTable table, final int... widths)
  3. setOptimalColumnWidth(final JTable table, final int col)
  4. SetPreferedColumnWIdth(JTable table, int[] widths)
  5. setPreferredColumnWidths(final JTable table, final float[] columnWeigths, final boolean includeHeader)
  6. setRelativeColumnWidths(JTable table, int... widths)
  7. setTableColWidth(JTable table, int column, int width)
  8. setTableColWidths(JTable table, int... colWidths)
  9. updateTableColumnWidth(JTable table)