Java JTable Column Width Set setColumnWidth(JTable table, int[] colWidth)

Here you can find the source of setColumnWidth(JTable table, int[] colWidth)

Description

Set the widths for the columns of table

License

Open Source License

Parameter

Parameter Description
table table to set the column width
colWidth array with the new column width

Declaration

public static void setColumnWidth(JTable table, int[] colWidth) 

Method Source Code

//package com.java2s;
/*/*  www . j  a va  2 s . c  o  m*/
 * Copyright (C) 2008 Michael Romankiewicz
 * microm at users.sourceforge.net
 *
 * 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 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.
 */

import javax.swing.JTable;

import javax.swing.table.TableColumnModel;

public class Main {
    /**
     * Set the widths for the columns of table
     * 
     * @param table
     *           table to set the column width
     * @param colWidth
     *           array with the new column width
     */
    public static void setColumnWidth(JTable table, int[] colWidth) {
        TableColumnModel tcm = table.getColumnModel();
        for (int col = 0; col < tcm.getColumnCount(); col++) {
            if (col < colWidth.length) {
                tcm.getColumn(col).setPreferredWidth(colWidth[col]);
            }
        }
        table.doLayout();
    }
}

Related

  1. setColumnsSize(TableColumnModel cm, int tablesize)
  2. setColumnWidth(JTable table, int columnIdx, int width)
  3. setColumnWidth(JTable table, int columnIndex, int preferredWidth)
  4. setColumnWidth(JTable table, int columnNumber, int width)
  5. setColumnWidth(JTable table, int... width)
  6. setColumnWidths(int[] preferredColWidths, int[] maxColWidths, int[] minColWidths, TableColumnModel columnModel, boolean[] columnsShowing)
  7. setColumnWidths(JTable p_Table, int[] p_ColumnWidths)
  8. setColumnWidths(JTable table, Insets insets, boolean setMinimum, boolean setMaximum)
  9. setColumnWidths(JTable table, Insets insets, boolean setMinimum, boolean setMaximum)