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

voidadjustColumnPreferredWidths(JTable table)
http://niravjavadeveloper.blogspot.com/2011/05/resize-jtable-columns.html
TableColumnModel columnModel = table.getColumnModel();
for (int col = 0; col < table.getColumnCount(); col++) {
    int maxwidth = 0;
    for (int row = 0; row < table.getRowCount(); row++) {
        TableCellRenderer rend = table.getCellRenderer(row, col);
        Object value = table.getValueAt(row, col) + "   ";
        Component comp = rend.getTableCellRendererComponent(table, value, false, false, row, col);
        maxwidth = Math.max(comp.getPreferredSize().width, maxwidth);
...
voidadjustColumnPreferredWidths(JTable table)
adjust Column Preferred Widths
TableColumnModel columnModel = table.getColumnModel();
for (int col = 0; col < table.getColumnCount(); col++) {
    int maxwidth = 0;
    for (int row = 0; row < table.getRowCount(); row++) {
        TableCellRenderer rend = table.getCellRenderer(row, col);
        Object value = table.getValueAt(row, col);
        Component comp = rend.getTableCellRendererComponent(table, value, false, false, row, col);
        maxwidth = Math.max(comp.getPreferredSize().width, maxwidth);
...
voidadjustColumnWidth(JTable table, int buf)
adjust Column Width
int columncount = table.getColumnCount();
for (int i = 0; i < columncount; i++) {
    sizeWidthToFitData(table, i, buf);
voidadjustColumnWidth(JTable tbl, int col, int maxColumnWidth)
(originally from @class org.openstreetmap.josm.gui.preferences.SourceEditor) adjust the preferred width of column col to the maximum preferred width of the cells requires JTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
int maxwidth = 0;
for (int row = 0; row < tbl.getRowCount(); row++) {
    TableCellRenderer tcr = tbl.getCellRenderer(row, col);
    Object val = tbl.getValueAt(row, col);
    Component comp = tcr.getTableCellRendererComponent(tbl, val, false, false, row, col);
    maxwidth = Math.max(comp.getPreferredSize().width, maxwidth);
tbl.getColumnModel().getColumn(col).setPreferredWidth(Math.min(maxwidth + 10, maxColumnWidth));
...
voidadjustColumnWidth(TableModel model, int columnIndex, int maxWidth, int rightPadding, JTable table)
It will adjust the column width to match the widest element.
if (columnIndex > model.getColumnCount() - 1) {
    return;
if (!model.getColumnClass(columnIndex).equals(String.class)) {
    return;
String longestValue = "";
for (int row = 0; row < model.getRowCount(); row++) {
...
voidadjustColWidth(JTable table, int firstColumnWidth, int middleColumnWidth, int lastColumnWidth, int padding)
Makes each column of the given table exactly the right width, so that the first and the last columns may have width values different from the middle ones.
int colCount = table.getColumnCount();
fitColumnWidth(table, 0, firstColumnWidth, firstColumnWidth, padding);
for (int i = 1; i < colCount - 1; i++) {
    fitColumnWidth(table, i, middleColumnWidth, middleColumnWidth, padding);
fitColumnWidth(table, colCount - 1, lastColumnWidth, lastColumnWidth, padding);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
DimensionautoFitColumnWidth(JTable table, TableColumn tableColumn)
auto Fit Column Width
final int margin = 5;
Dimension headerFit = getHeaderDimension(table, tableColumn);
int width = headerFit.width;
int height = 0;
int order = table.convertColumnIndexToView(tableColumn.getModelIndex());
for (int r = 0; r < table.getRowCount(); r++) {
    TableCellRenderer renderer = table.getCellRenderer(r, order);
    Component comp = renderer.getTableCellRendererComponent(table, table.getValueAt(r, order), false, false,
...
JTableautoResizeColWidth(final JTable table, final DefaultTableModel model)
auto Resize Col Width
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table.setModel(model);
int margin = 5;
DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel();
int preferredWidthTotal = 0;
int renderedWidthTotal = 0;
int[] colWidths = new int[table.getColumnCount()];
for (int i = 0; i < table.getColumnCount(); i++) {
...
JTableautoResizeColWidth(JTable table, AbstractTableModel model)
Resizes the table columns based on the column and data preferred widths.
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table.setModel(model);
int margin = 5;
for (int i = 0; i < table.getColumnCount(); i++) {
    int vColIndex = i;
    DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel();
    TableColumn col = colModel.getColumn(vColIndex);
    int width;
...
JTableautoResizeColWidth(JTable table, int extraSpaceToColumn)
auto Resize Col Width
if (extraSpaceToColumn < 0 || extraSpaceToColumn >= table.getColumnCount()) {
    throw new IllegalArgumentException("Illegal Column index.  Table " + table.getName() + " has "
            + table.getColumnCount() + " columns.  Can't set extra space to column " + extraSpaceToColumn);
int totalWidth = autoResizeColWidthNoFill(table);
int availableWidth = table.getParent().getWidth();
if (totalWidth >= availableWidth) {
    return table;
...