Here you can find the source of chooseGoodColumnWidths(JTable table)
Parameter | Description |
---|---|
table | Description of the Parameter |
public static void chooseGoodColumnWidths(JTable table)
//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); } } }