Java JTable Column Width Set chooseGoodColumnWidths(JTable table)

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

Description

Sets the column widths based on the first row.

License

Open Source License

Parameter

Parameter Description
table Description of the Parameter

Declaration

public static void chooseGoodColumnWidths(JTable table) 

Method Source Code

//package com.java2s;
/*/* w ww .  j  a  v  a 2s.co m*/
 * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
 * for visualizing and manipulating spatial features with geometry and attributes.
 *
 * Copyright (C) 2003 Vivid Solutions
 *
 * This program 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.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * For more information, contact:
 *
 * Vivid Solutions
 * Suite #1A
 * 2328 Government Street
 * Victoria BC  V8T 5G5
 * Canada
 *
 * (250)385-6040
 * www.vividsolutions.com
 */

import javax.swing.*;

import javax.swing.table.TableColumn;

public class Main {
    /**
     * Sets the column widths based on the first row.
     * 
     * @param table
     *                   Description of the Parameter
     */
    public static void chooseGoodColumnWidths(JTable table) {
        //Without padding, columns are slightly narrow, and we get "...". [Jon
        // Aquino]
        final int PADDING = 5;

        if (table.getModel().getRowCount() == 0) {
            return;
        }

        for (int i = 0; i < table.getModel().getColumnCount(); i++) {
            TableColumn column = table.getColumnModel().getColumn(i);
            double headerWidth = table.getTableHeader().getDefaultRenderer()
                    .getTableCellRendererComponent(table, table.getModel().getColumnName(i), false, false, 0, i)
                    .getPreferredSize().getWidth() + PADDING;
            double valueWidth = 10; // default in case of error

            try {
                valueWidth = table.getCellRenderer(0, i)
                        .getTableCellRendererComponent(table, table.getModel().getValueAt(0, i), false, false, 0, i)
                        .getPreferredSize().getWidth() + PADDING;
            } catch (Exception ex) {
                // ignore the exception, since we can easily choose a default
                // width
            }

            //Limit column width to 200 pixels.
            int width = Math.min(200, Math.max((int) headerWidth, (int) valueWidth));
            column.setPreferredWidth(width);

            //Need to set the actual width too, otherwise actual width may end
            //up a bit less than the preferred width. [Jon Aquino]
            column.setWidth(width);
        }
    }
}

Related

  1. autoResizeColWidth(final JTable table, final DefaultTableModel model)
  2. autoResizeColWidth(JTable table, AbstractTableModel model)
  3. autoResizeColWidth(JTable table, int extraSpaceToColumn)
  4. autoResizeColWidthNoFill(JTable table)
  5. calcColumnWidth(int col)
  6. fitColumnWidth(JTable table, int colIndex, int padding)
  7. fitColumnWidths(JTable table, int padding)
  8. fitColumnWidths(TableModel model, JTable mainTable)
  9. getPreferredWidthForColumn(TableColumn col, JTable table)