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

voidinitTableWidth(JTable table, int[] colWiths)
init Table Width
if (table.getColumnModel().getColumnCount() != colWiths.length) {
    throw new IllegalArgumentException("Number of Column widths doesn't matched!");
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
for (int i = 0; i < colWiths.length; i++) {
    TableColumn col = table.getColumnModel().getColumn(i);
    col.setPreferredWidth(colWiths[i]);
voidmakeTableColumnWidthFit(JTable jTable, int col, int margin)
make Table Column Width Fit
makeTableColumnWidthFit(jTable, col, margin, false);
voidsetColumnMaxWidths(JTable tbl, Integer... widths)
set Column Max Widths
TableColumnModel columnModel = tbl.getColumnModel();
for (int i = 0; i < widths.length; i++) {
    if (widths[i] > -1) {
        columnModel.getColumn(i).setMaxWidth(widths[i]);
voidsetColumnMinWidths(final JTable table)
set the widths of all of the columns to the max width of the object in the column.
final TableColumnModel colModel = table.getColumnModel();
final int columns = colModel.getColumnCount();
for (int columnIndex = 0; columnIndex < columns; columnIndex++) {
    int maxWidth = 0;
    final TableColumn column = colModel.getColumn(columnIndex);
    TableCellRenderer renderer = column.getCellRenderer();
    if (renderer == null) {
        renderer = table.getDefaultRenderer(table.getColumnClass(columnIndex));
...
voidsetColumnSize(TableColumnModel cm, int id, int percent, int tablesize, boolean resizeable)
set Column Size
TableColumn column = cm.getColumn(id);
column.setMinWidth(1);
column.setPreferredWidth(tablesize * percent / 100);
column.setMaxWidth(tablesize);
voidsetColumnSizes(JTable table, double[] percentages)
set Column Sizes
Dimension tableDim = table.getPreferredSize();
tableDim = table.getSize();
double total = 0;
for (double t : percentages)
    total += t;
table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
for (int i = 0; i < table.getColumnModel().getColumnCount(); i++) {
    TableColumn column = table.getColumnModel().getColumn(i);
...
voidsetColumnsSize(TableColumnModel cm, int tablesize)
set Columns Size
boolean resizeable = false;
setColumnSize(cm, FEED_NAME, COLUMN_SIZE_PERCENT_FEED_NAME, tablesize, resizeable);
setColumnSize(cm, FEED_URL, COLUMN_SIZE_PERCENT_FEED_URL, tablesize, resizeable);
setColumnSize(cm, INTERVALL, COLUMN_SIZE_PERCENT_INTERVALL, tablesize, resizeable);
setColumnSize(cm, MONITORED, COLUMN_SIZE_PERCENT_MONITORED, tablesize, resizeable);
voidsetColumnWidth(JTable table, int columnIdx, int width)
set Column Width
TableColumn o = table.getColumnModel().getColumn(columnIdx);
o.setWidth(width);
o.setMaxWidth(width);
o.setMinWidth(width);
voidsetColumnWidth(JTable table, int columnIndex, int preferredWidth)
set Column Width
TableColumn column = getTableColumn(table, columnIndex);
if (column == null) {
    return;
column.setPreferredWidth(preferredWidth);
voidsetColumnWidth(JTable table, int columnNumber, int width)
set Column Width
TableColumn column = table.getColumnModel().getColumn(columnNumber);
column.setPreferredWidth(width);
column.setWidth(width);