List of usage examples for javax.swing JTable getRowMargin
public int getRowMargin()
From source file:Main.java
/** * Returns the visual height of the given table. * * @param table the table.// ww w . ja va 2 s .c o m * * @return the table height. */ public static int getTableHeight(JTable table) { int result = 0; int rowHeight = 0; for (int i = 0, rows = table.getRowCount(); i < rows; i++) { int height = table.getRowHeight(i); result += height; if (height > rowHeight) { rowHeight = height; } } return result + rowHeight + (table.getRowCount() * table.getRowMargin()); }
From source file:Main.java
/** * Auto fit the column of a table.// w ww . j a v a2s . c o m * @param table the table for which to auto fit the columns. * @param columnIndex the index of the column to auto fit. * @param maxWidth the maximum width that a column can take (like Integer.MAX_WIDTH). */ public static void autoFitTableColumn(JTable table, int columnIndex, int maxWidth) { TableModel model = table.getModel(); TableCellRenderer headerRenderer = table.getTableHeader().getDefaultRenderer(); int rowCount = table.getRowCount(); for (int i = columnIndex >= 0 ? columnIndex : model.getColumnCount() - 1; i >= 0; i--) { TableColumn column = table.getColumnModel().getColumn(i); int headerWidth = headerRenderer .getTableCellRendererComponent(table, column.getHeaderValue(), false, false, 0, 0) .getPreferredSize().width; int cellWidth = 0; for (int j = 0; j < rowCount; j++) { Component comp = table.getDefaultRenderer(model.getColumnClass(i)) .getTableCellRendererComponent(table, table.getValueAt(j, i), false, false, 0, i); int preferredWidth = comp.getPreferredSize().width; // Artificial space to look nicer. preferredWidth += 10; cellWidth = Math.max(cellWidth, preferredWidth); } // Artificial space for the sort icon. headerWidth += 20; column.setPreferredWidth(Math.min(Math.max(headerWidth, cellWidth) + table.getRowMargin(), maxWidth)); if (columnIndex >= 0) { break; } } }