Here you can find the source of packRows(JTable table, int start, int end, int margin)
private static void packRows(JTable table, int start, int end, int margin)
//package com.java2s; /*/*from w w w .j a v a 2 s.c o m*/ Copyright 2011 Udayakumar Dhansingh (Udy) 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 http://www.apache.org/licenses/LICENSE-2.0 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 java.awt.Component; import javax.swing.JTable; import javax.swing.table.TableCellRenderer; public class Main { public static void packRows(JTable table, int margin) { packRows(table, 0, table.getRowCount(), margin); } private static void packRows(JTable table, int start, int end, int margin) { for (int r = 0; r < table.getRowCount(); r++) { // Get the preferred height int h = getPreferredRowHeight(table, r, margin); // Now set the row height using the preferred height if (table.getRowHeight(r) != h) { table.setRowHeight(r, h); } } } private static int getPreferredRowHeight(JTable table, int rowIndex, int margin) { // Get the current default height for all rows int height = table.getRowHeight(); // Determine highest cell in the row for (int c = 0; c < table.getColumnCount(); c++) { TableCellRenderer renderer = table.getCellRenderer(rowIndex, c); Component comp = table.prepareRenderer(renderer, rowIndex, c); int h = comp.getPreferredSize().height + 2 * margin; height = Math.max(height, h); } return height; } }