Java JTable Column Width Set updateTableColumnWidth(JTable table)

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

Description

Calls updateTableColumnWidth(table, 5000).

License

Open Source License

Parameter

Parameter Description
table a parameter

Declaration

public static void updateTableColumnWidth(JTable table) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import javax.swing.JTable;

public class Main {
    /**/*  w  ww.  j a  v  a 2 s.c om*/
     * Calls <code>updateTableColumnWidth(table, 5000)</code>.
     * @param table 
     */
    public static void updateTableColumnWidth(JTable table) {
        updateTableColumnWidth(table, 5000);
    }

    /**
     * Updates the width of each column according to the table size and the data in the cells.
     * @param table
     * @param maxRows maximum number of rows to be taken in considerations.
     */
    public static void updateTableColumnWidth(JTable table, int maxRows) {
        int tableWidth = table.getWidth();
        if (table.getAutoResizeMode() == JTable.AUTO_RESIZE_OFF) {
            tableWidth = Integer.MAX_VALUE;
        }
        int colsum = 0;
        int width[] = new int[table.getColumnCount()];
        int minwidth[] = new int[table.getColumnCount()];
        int minwidthsum = 0;
        // iterate over all not filtered cells and get the length from each one.
        // the maximum of the lengths of all cells within a column col will be in width[col]
        if (table.getRowCount() == 0) {
            for (int i = 0; i < table.getColumnCount(); i++) {
                width[i] = table.getWidth() / table.getColumnCount();
            }
        } else {
            for (int col = 0; col < table.getColumnCount(); col++) {
                // set the width to the columns title width
                if (table.getColumnModel().getColumn(col).getHeaderRenderer() != null) {
                    width[col] = table.getColumnModel().getColumn(col).getHeaderRenderer()
                            .getTableCellRendererComponent(table,
                                    table.getColumnModel().getColumn(col).getHeaderValue(), false, false, 0, 0)
                            .getPreferredSize().width + 10;
                } else {
                    width[col] = table.getDefaultRenderer(String.class)
                            .getTableCellRendererComponent(table,
                                    table.getColumnModel().getColumn(col).getHeaderValue(), false, false, 0, 0)
                            .getPreferredSize().width + 10;
                }
                minwidth[col] = width[col];
                minwidthsum += width[col];
                for (int row = 0; row < Math.min(maxRows, table.getRowCount()); row++) {
                    // get the component which represents the value and determine its witdth
                    int len = table.getCellRenderer(row, col)
                            .getTableCellRendererComponent(table, table.getValueAt(row, col), false, true, row, col)
                            .getPreferredSize().width;
                    if (len > width[col]) {
                        width[col] = len + 5;
                    }
                }
                width[col] += table.getIntercellSpacing().width;
                colsum += width[col];
            }
        }
        for (int col = 0; col < table.getColumnCount(); col++) {
            double proz;
            // get the weight for a pixel
            if (table.getAutoResizeMode() == JTable.AUTO_RESIZE_OFF) {
                proz = 1.;
            } else {

                proz = (double) tableWidth / (double) colsum;
            }
            // multiplicate the width of a column with the weight
            int w = (int) Math.round(proz * width[col]);
            if (w < minwidth[col]) {
                w = minwidth[col];
            }
            if (tableWidth - w < minwidthsum - minwidth[col]) {
                w = tableWidth - minwidthsum;
            }
            table.getColumnModel().getColumn(col).setPreferredWidth(w);
            minwidthsum -= minwidth[col];
            tableWidth -= w;
        }
    }
}

Related

  1. setPreferredColumnWidths(final JTable table, final float[] columnWeigths, final boolean includeHeader)
  2. setPreferredRowHeights(final JTable table)
  3. setRelativeColumnWidths(JTable table, int... widths)
  4. setTableColWidth(JTable table, int column, int width)
  5. setTableColWidths(JTable table, int... colWidths)