Java JTable Column Pack packColumns(JTable table)

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

Description

Adjusts the widths of columns to be just wide enough to show all of the column headers and the widest cells in the columns

License

Open Source License

Parameter

Parameter Description
table Table for which the columns widths must be adjusted

Declaration

public static void packColumns(JTable table) 

Method Source Code


//package com.java2s;
/*// ww  w.j  av  a2  s  . c  o m
 * Utilities.java
 *
 * Copyright (C) 2009 Francois Duchemin
 *
 * This file is part of GrisbiGraphs.
 *
 * GrisbiGraphs is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * GrisbiGraphs is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GrisbiGraphs; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

import java.awt.Component;

import javax.swing.JTable;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;

public class Main {
    /**
     * Adjusts the widths of columns to be just wide enough to show all of
     * the column headers and the widest cells in the columns
     * @param table Table for which the columns widths must be adjusted
     */
    public static void packColumns(JTable table) {
        for (int columnIndex = 0; columnIndex < table.getColumnCount(); columnIndex++) {
            packColumn(table, columnIndex, 2);
        }
    }

    /**
     * Sets the preferred width of the visible column specified by vColIndex. The column
     * will be just wide enough to show the column head and the widest cell in the column.
     * Margin pixels are added to the left and right.
     * @param table Table for which the column width must be adjusted
     * @param vColIndex Column index
     * @param margin Margin to add left and right
     */
    private static void packColumn(JTable table, int vColIndex, int margin) {
        DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel();
        TableColumn col = colModel.getColumn(vColIndex);
        int width = 0;

        // Get width of column header
        TableCellRenderer renderer = col.getHeaderRenderer();
        if (renderer == null) {
            renderer = table.getTableHeader().getDefaultRenderer();
        }
        Component comp = renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false, false, 0, 0);
        width = comp.getPreferredSize().width;

        // Get maximum width of column data
        for (int r = 0; r < table.getRowCount(); r++) {
            renderer = table.getCellRenderer(r, vColIndex);
            comp = renderer.getTableCellRendererComponent(table, table.getValueAt(r, vColIndex), false, false, r,
                    vColIndex);
            width = Math.max(width, comp.getPreferredSize().width);
        }

        // Add margin
        width += 2 * margin;

        // Set the width
        col.setPreferredWidth(width);
    }
}

Related

  1. packColumn(JTable table, int vColIndex, int margin)
  2. packColumn(JTable table, int vColIndex, int margin)
  3. packColumn(JTable table, int vColIndex, int margin)
  4. packColumn(JTable table, int vColIndex, int margin)
  5. packColumns(final JTable table, final int margin)
  6. packColumns(JTable table, int margin)
  7. packColumns(JTable table, int width)
  8. packColumnsWidthFixedFirst(JTable table, int margin)