Here you can find the source of autoResizeTableRowHeights(JTable table)
Parameter | Description |
---|---|
table | table to be resized |
static public void autoResizeTableRowHeights(JTable table)
//package com.java2s; import java.awt.Component; import java.awt.Dimension; import javax.swing.JTable; import javax.swing.table.DefaultTableColumnModel; import javax.swing.table.TableCellRenderer; import javax.swing.table.TableColumn; public class Main { /**//from w w w .j a v a2 s. co m * Adjust the heights of each row of a table to match the max height of each * value in the table. * * @param table table to be resized */ static public void autoResizeTableRowHeights(JTable table) { table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); for (int rowIndex = 0; rowIndex < table.getRowCount(); rowIndex++) { int height = 0; for (int colIndex = 0; colIndex < table.getColumnCount(); colIndex++) { DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel(); TableColumn col = colModel.getColumn(colIndex); TableCellRenderer renderer = table.getCellRenderer(rowIndex, colIndex); Object value = table.getValueAt(rowIndex, colIndex); if (null != value) { Component comp = renderer.getTableCellRendererComponent(table, value, false, false, rowIndex, colIndex); Dimension compSize = comp.getPreferredSize(); int thisCompHeight = compSize.height; height = Math.max(height, thisCompHeight); } } if (height > 0) { table.setRowHeight(rowIndex, height); } } } }