Java JTable Column Size Calculate autoSizeCols(JTable table)

Here you can find the source of autoSizeCols(JTable table)

Description

auto Size Cols

License

Apache License

Declaration

public static void autoSizeCols(JTable table) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.awt.Component;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

public class Main {
    public static void autoSizeCols(JTable table) {
        if (table == null || table.getColumnCount() == 0) {
            return;
        }//w  ww  .  ja  v  a  2  s .  c om

        // strategy - get max width for cells in column and
        // make that the preferred width
        TableColumnModel columnModel = table.getColumnModel();

        // For each col
        for (int col = 0; col < table.getColumnCount(); col++) {
            int maxwidth = 0;

            // For each row
            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);
            }

            TableColumn column = columnModel.getColumn(col);
            TableCellRenderer headerRenderer = column.getHeaderRenderer();
            if (headerRenderer == null) {
                headerRenderer = table.getTableHeader().getDefaultRenderer();
            }
            Object headerValue = column.getHeaderValue();
            Component headerComp = headerRenderer.getTableCellRendererComponent(table, headerValue, false, false, 0,
                    col);
            maxwidth = Math.max(maxwidth, headerComp.getPreferredSize().width);

            column.setPreferredWidth(maxwidth);
        }
    }
}

Related

  1. adjustColumnSizes(JTable table, int column, int margin)
  2. autoSizeTableColumns(JTable table)
  3. calcColumnWidth(JTable table, int col)
  4. calcColumnWidths(final JTable table)
  5. calcColumnWidths(final JTable table)