Here you can find the source of setColumnWidths(int[] preferredColWidths, int[] maxColWidths, int[] minColWidths, TableColumnModel columnModel, boolean[] columnsShowing)
public static void setColumnWidths(int[] preferredColWidths, int[] maxColWidths, int[] minColWidths, TableColumnModel columnModel, boolean[] columnsShowing)
//package com.java2s; /*/*from w ww. j av a 2s. co m*/ * DBSchools * Copyright (C) 2005 David C. Briccetti * www.davebsoft.com * * 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 */ import java.util.ArrayList; import java.util.List; import javax.swing.table.TableColumn; import javax.swing.table.TableColumnModel; public class Main { public static void setColumnWidths(int[] preferredColWidths, int[] maxColWidths, int[] minColWidths, TableColumnModel columnModel, boolean[] columnsShowing) { List<Integer> visibleColumns = visibleColumnsMap(columnsShowing, preferredColWidths.length); assert visibleColumns.size() == columnModel.getColumnCount(); for (int visColIdx = 0; visColIdx < columnModel.getColumnCount(); ++visColIdx) { int allColsIdx = visibleColumns.get(visColIdx); TableColumn column = columnModel.getColumn(visColIdx); column.setPreferredWidth(preferredColWidths[allColsIdx]); if (maxColWidths[allColsIdx] > 0) { column.setMaxWidth(maxColWidths[allColsIdx]); } if (minColWidths[allColsIdx] > 0) { column.setMinWidth(minColWidths[allColsIdx]); } } } public static List<Integer> visibleColumnsMap(boolean[] columnsShowing, int len) { List<Integer> visibleColumns = new ArrayList<Integer>(); for (int i = 0; i < len; ++i) { if (columnsShowing == null || columnsShowing[i]) { visibleColumns.add(i); } } return visibleColumns; } }