void () { final JTableHeader header = getTableHeader(); final TableCellRenderer defaultRenderer = header == null? null : header.getDefaultRenderer(); final RowSorter<? extends TableModel> sorter = getRowSorter(); final RowSorter.SortKey sortKey = sorter == null ? null : ContainerUtil.getFirstItem(sorter.getSortKeys()); ColumnInfo[] columnInfos = getListTableModel().getColumnInfos(); TableColumnModel columnModel = getColumnModel(); int visibleColumnCount = columnModel.getColumnCount(); int[] sizeMode = new int[visibleColumnCount]; int[] headers = new int[visibleColumnCount]; int[] widths = new int[visibleColumnCount]; int allColumnWidth = 0; int allColumnCurrent = 0; int varCount = 0; Icon sortIcon = UIManager.getIcon("Table.ascendingSortIcon"); Border border = UIManager.getBorder("Table.cellNoFocusBorder"); Insets borderInsets = border == null ? null : border.getBorderInsets(this); int borderWidth = borderInsets == null ? 0 : borderInsets.left + borderInsets.right; if (getShowVerticalLines()) borderWidth += 2; // calculate for (int i = 0; i < visibleColumnCount; i++) { final TableColumn column = columnModel.getColumn(i); final ColumnInfo columnInfo = columnInfos[column.getModelIndex()]; TableCellRenderer columnHeaderRenderer = column.getHeaderRenderer(); if (columnHeaderRenderer == null) { columnHeaderRenderer = defaultRenderer; } final Component headerComponent = columnHeaderRenderer == null? null : columnHeaderRenderer.getTableCellRendererComponent(this, column.getHeaderValue(), false, false, 0, i); if (headerComponent != null) { headers[i] = headerComponent.getPreferredSize().width; // add sort icon width if (sorter != null && columnInfo.isSortable() && sortIcon != null && (sortKey == null || sortKey.getColumn() != i)) { headers[i] += sortIcon.getIconWidth() + (headerComponent instanceof JLabel? ((JLabel)headerComponent).getIconTextGap() : 0); } } final String maxStringValue; final String preferredValue; if (columnInfo.getWidth(this) > 0) { sizeMode[i] = 1; int width = columnInfo.getWidth(this); widths[i] = width; } else if ((maxStringValue = columnInfo.getMaxStringValue()) != null) { sizeMode[i] = 2; widths[i] = getFontMetrics(getFont()).stringWidth(maxStringValue) + columnInfo.getAdditionalWidth(); varCount ++; } else if ((preferredValue = columnInfo.getPreferredStringValue()) != null) { sizeMode[i] = 3; widths[i] = getFontMetrics(getFont()).stringWidth(preferredValue) + columnInfo.getAdditionalWidth(); varCount ++; } widths[i] += borderWidth; allColumnWidth += widths[i]; allColumnCurrent += column.getPreferredWidth(); } allColumnWidth = Math.max(allColumnWidth, allColumnCurrent); // apply: distribute available space between resizable columns // and make sure that header will fit as well int viewWidth = getParent() != null? getParent().getWidth() : getWidth(); double gold = 0.5 * (3 - Math.sqrt(5)); int addendum = varCount == 0 || viewWidth < allColumnWidth ? 0 : (int)((allColumnWidth < gold * viewWidth ? gold * viewWidth : allColumnWidth < (1 - gold) * viewWidth ? (1 - gold) * viewWidth : viewWidth) - allColumnWidth) / varCount; for (int i = 0 ; i < visibleColumnCount; i++) { TableColumn column = columnModel.getColumn(i); int width = widths[i]; if (sizeMode[i] == 1) { column.setMaxWidth(width); column.setPreferredWidth(width); column.setMinWidth(width); } else if (sizeMode[i] == 2) { // do not shrink columns width = Math.max(column.getPreferredWidth(), Math.max(width + addendum, headers[i])); column.setPreferredWidth(width); column.setMaxWidth(width); } else if (sizeMode[i] == 3) { // do not shrink columns width = Math.max(column.getPreferredWidth(), Math.max(width + addendum, headers[i])); column.setPreferredWidth(width); } } }