Java Utililty Methods JTable Column Width Set

List of utility methods to do JTable Column Width Set

Description

The list of methods to do JTable Column Width Set are organized into topic(s).

Method

voidsetPreferredColumnWidths(final JTable table, final float[] columnWeigths, final boolean includeHeader)
This method will iterate over the given table's columns and attempt to find the optimal width for each.
if (table == null)
    throw new IllegalArgumentException("Parameter 'table' must not be null!");
if (columnWeigths == null)
    throw new IllegalArgumentException("Parameter 'columnWeigths' must not be null!");
final int columnCount = table.getColumnCount();
final int rowCount = table.getRowCount();
if (columnWeigths != null && columnWeigths.length != columnCount)
    throw new IllegalArgumentException(
...
voidsetPreferredRowHeights(final JTable table)
This method will iterate over the given table's rows and attempt to find the optimal height for each.
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,
...
voidsetRelativeColumnWidths(JTable table, int... widths)
Sets the preferred widths of the columns of a table to the specified percentages of the current width.
int tableWidth = table.getWidth();
int usedWidth = 0;
TableColumnModel model = table.getColumnModel();
for (int idx = 0; idx < Math.min(widths.length, model.getColumnCount()); idx++) {
    TableColumn col = model.getColumn(idx);
    int colWidth = tableWidth * widths[idx] / 100;
    col.setPreferredWidth(colWidth);
    usedWidth += colWidth;
...
TableColumnsetTableColWidth(JTable table, int column, int width)
Sets the width of column to fixed value.
TableColumn col = table.getColumnModel().getColumn(column);
col.setMinWidth(width);
col.setMaxWidth(width);
return col;
voidsetTableColWidths(JTable table, int... colWidths)
set Table Col Widths
if ((table != null) && (colWidths != null)) {
    TableColumnModel tcm = table.getColumnModel();
    if (tcm != null) {
        int n = Math.min(tcm.getColumnCount(), colWidths.length);
        for (int i = 0; i < n; i++) {
            TableColumn c = tcm.getColumn(i);
            if (c != null)
                c.setPreferredWidth(colWidths[i]);
...
voidupdateTableColumnWidth(JTable table)
Calls updateTableColumnWidth(table, 5000).
updateTableColumnWidth(table, 5000);