public AlignmentRenderer(int alignment) this.alignment = alignment;
public void fastAutoSizeColumns(JTable table, int sampleRowLimit) int rowCount = Math.min(table.getRowCount(), sampleRowLimit); TableColumnModel colModel = table.getColumnModel(); for (int col = 0; col < table.getColumnCount(); col++) int maxWidth = 0; for (int row = 0; row < rowCount; row++) TableCellRenderer renderer = table.getCellRenderer(row, col); Component comp = table.prepareRenderer(renderer, row, col); maxWidth = Math.max(maxWidth, comp.getPreferredSize().width); Java Swing - JTable Text Alignment And Column W...
If you load an AbstractTableModel with a list of sales figures (e.g., $1299.99 , $5.00 ), the numbers will appear left-aligned, breaking user expectations. Similarly, a column containing long product descriptions will be cut off ( "This is a very long descri..." ). public AlignmentRenderer(int alignment) this
public class TableColumnWidthExample public static void main(String[] args) // Create a new JFrame JFrame frame = new JFrame("Table Column Width Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); public void fastAutoSizeColumns(JTable table