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

voidsetColumnWidth(JTable table, int... width)
set Column Width
TableColumnModel columnModel = table.getColumnModel();
int length = width.length;
int columnCount = table.getColumnCount();
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
for (int i = 0; i < length && i < columnCount; i++) {
    columnModel.getColumn(i).setPreferredWidth(width[i]);
voidsetColumnWidth(JTable table, int[] colWidth)
Set the widths for the columns of table
TableColumnModel tcm = table.getColumnModel();
for (int col = 0; col < tcm.getColumnCount(); col++) {
    if (col < colWidth.length) {
        tcm.getColumn(col).setPreferredWidth(colWidth[col]);
table.doLayout();
voidsetColumnWidths(int[] preferredColWidths, int[] maxColWidths, int[] minColWidths, TableColumnModel columnModel, boolean[] columnsShowing)
set Column Widths
List<Integer> visibleColumns = visibleColumnsMap(columnsShowing, preferredColWidths.length);
assert visibleColumns.size() == columnModel.getColumnCount();
for (int visColIdx = 0; visColIdx < columnModel.getColumnCount(); ++visColIdx) {
    int allColsIdx = visibleColumns.get(visColIdx);
    TableColumn column = columnModel.getColumn(visColIdx);
    column.setPreferredWidth(preferredColWidths[allColsIdx]);
    if (maxColWidths[allColsIdx] > 0) {
        column.setMaxWidth(maxColWidths[allColsIdx]);
...
voidsetColumnWidths(JTable p_Table, int[] p_ColumnWidths)
set Column Widths
TableColumnModel columns = p_Table.getTableHeader().getColumnModel();
if (p_ColumnWidths == null || p_ColumnWidths.length != columns.getColumnCount()) {
    return;
for (int i = 0, c = columns.getColumnCount(); i < c; i++) {
    columns.getColumn(i).setPreferredWidth(p_ColumnWidths[i]);
p_Table.getTableHeader().resizeAndRepaint();
...
voidsetColumnWidths(JTable table, Insets insets, boolean setMinimum, boolean setMaximum)
set Column Widths
int columnCount = table.getColumnCount();
TableColumnModel tcm = table.getColumnModel();
int spare = (insets == null ? 0 : insets.left + insets.right);
for (int i = 0; i < columnCount; i++) {
    int width = calculateColumnWidth(table, i);
    width += spare;
    TableColumn column = tcm.getColumn(i);
    column.setPreferredWidth(width);
...
voidsetColumnWidths(JTable table, Insets insets, boolean setMinimum, boolean setMaximum)
Sets the columnWidths attribute of the TableUtilities class
boolean done = false;
int retries = 0;
do {
    try {
        int columnCount = table.getColumnCount();
        TableColumnModel tcm = table.getColumnModel();
        int spare = (insets == null ? 0 : insets.left + insets.right);
        for (int i = 0; i < columnCount; i++) {
...
voidsetDefaultColumnWidth(JTable table, int column)
Sets the width of the given JTable column to the width of its widest contained value.
int resizeMode = table.getAutoResizeMode();
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
TableCellRenderer renderer = table.getCellRenderer(0, column);
int max = 0;
TableModel tableModel = table.getModel();
int rows = tableModel.getRowCount();
for (int i = 0; i < rows; i++) {
    Object obj = tableModel.getValueAt(i, column);
...
voidsetMaxnimumColumnWidths(final JTable table, final int... widths)
set Maxnimum Column Widths
TableColumnModel columnModel = table.getColumnModel();
for (int col = 0; col < widths.length; col++) {
    columnModel.getColumn(col).setMaxWidth(widths[col]);
voidsetOptimalColumnWidth(final JTable table, final int col)
sets the optimal column width for the given column.
final int width;
if ((col >= 0) && (col < table.getColumnModel().getColumnCount())) {
    width = calcColumnWidth(table, col);
    if (width >= 0) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JTableHeader header = table.getTableHeader();
...
voidSetPreferedColumnWIdth(JTable table, int[] widths)
Set Prefered Column W Idth
for (int i = 0; i < widths.length; i++) {
    table.getColumnModel().getColumn(i).setPreferredWidth(widths[i]);