Java JTable Column Width Set setColumnMinWidths(final JTable table)

Here you can find the source of setColumnMinWidths(final JTable table)

Description

set the widths of all of the columns to the max width of the object in the column.

License

Open Source License

Declaration

public static void setColumnMinWidths(final JTable table) 

Method Source Code


//package com.java2s;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

public class Main {
    /**// w w  w.j  av a 2 s  .com
     * set the widths of all of the columns to the max width of the object in the
     * column.
     * 
     * @pre (table != null)
     **/
    public static void setColumnMinWidths(final JTable table) {
        final TableColumnModel colModel = table.getColumnModel();
        /**
         * @assert (colModel.getColumnCount() == table.getColumnCount()),
         *         "table and column model don't agree";
         **/
        final int columns = colModel.getColumnCount();

        for (int columnIndex = 0; columnIndex < columns; columnIndex++) {
            int maxWidth = 0;
            final TableColumn column = colModel.getColumn(columnIndex);
            /**
             * @assert (column != null)
             **/
            TableCellRenderer renderer = column.getCellRenderer();

            if (renderer == null) {
                renderer = table.getDefaultRenderer(table.getColumnClass(columnIndex));
            }
            if (renderer != null) {
                final int rows = table.getRowCount();
                for (int rowIndex = 0; rowIndex < rows; rowIndex++) {
                    final Object object = table.getValueAt(rowIndex, columnIndex);
                    maxWidth = Math.max(maxWidth, renderer
                            .getTableCellRendererComponent(table, object, false, false, rowIndex, columnIndex)
                            .getPreferredSize().width);
                }
                // ?????
                // maxWidth = Math.max(maxWidth, column.getWidth());

                renderer = column.getHeaderRenderer();
                if (renderer == null) {
                    renderer = table.getTableHeader().getDefaultRenderer();
                }
                maxWidth = Math.max(maxWidth, renderer.getTableCellRendererComponent(table, column.getHeaderValue(),
                        false, false, -1, columnIndex).getPreferredSize().width);
                column.setMinWidth(maxWidth + 5);
            }
        }
    }
}

Related

  1. getRenderedWidth(JTable table, int tableColumn, String value)
  2. getTotalColumnWidth(JTable table)
  3. initTableWidth(JTable table, int[] colWiths)
  4. makeTableColumnWidthFit(JTable jTable, int col, int margin)
  5. setColumnMaxWidths(JTable tbl, Integer... widths)
  6. setColumnSize(TableColumnModel cm, int id, int percent, int tablesize, boolean resizeable)
  7. setColumnSizes(JTable table, double[] percentages)
  8. setColumnsSize(TableColumnModel cm, int tablesize)
  9. setColumnWidth(JTable table, int columnIdx, int width)