Java JTable Column Width Set setColumnWidths(int[] preferredColWidths, int[] maxColWidths, int[] minColWidths, TableColumnModel columnModel, boolean[] columnsShowing)

Here you can find the source of setColumnWidths(int[] preferredColWidths, int[] maxColWidths, int[] minColWidths, TableColumnModel columnModel, boolean[] columnsShowing)

Description

set Column Widths

License

Open Source License

Declaration

public static void setColumnWidths(int[] preferredColWidths, int[] maxColWidths, int[] minColWidths,
            TableColumnModel columnModel, boolean[] columnsShowing) 

Method Source Code

//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;
    }
}

Related

  1. setColumnWidth(JTable table, int columnIdx, int width)
  2. setColumnWidth(JTable table, int columnIndex, int preferredWidth)
  3. setColumnWidth(JTable table, int columnNumber, int width)
  4. setColumnWidth(JTable table, int... width)
  5. setColumnWidth(JTable table, int[] colWidth)
  6. setColumnWidths(JTable p_Table, int[] p_ColumnWidths)
  7. setColumnWidths(JTable table, Insets insets, boolean setMinimum, boolean setMaximum)
  8. setColumnWidths(JTable table, Insets insets, boolean setMinimum, boolean setMaximum)
  9. setDefaultColumnWidth(JTable table, int column)