Here you can find the source of ensureColumnCount(int count, JTable table)
Parameter | Description |
---|---|
table | a parameter |
public static void ensureColumnCount(int count, JTable table)
//package com.java2s; /*//from w w w . ja v a 2s . c o m * Open-Source tuning tools * * 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., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ import javax.swing.JTable; import javax.swing.table.DefaultTableModel; public class Main { /** * Method checks if the column count is greater or equal to specified count, if not - it add a column * @param count, number of columns to check for * @param table */ public static void ensureColumnCount(int count, JTable table) { if (count <= table.getColumnCount()) return; int[] minwidth = new int[count]; int[] maxwidth = new int[count]; int[] prefwidth = new int[count]; int i, j; for (i = 0; i < table.getColumnCount(); ++i) { minwidth[i] = table.getColumnModel().getColumn(i).getMinWidth(); maxwidth[i] = table.getColumnModel().getColumn(i).getMaxWidth(); prefwidth[i] = table.getColumnModel().getColumn(i).getPreferredWidth(); } DefaultTableModel model = (DefaultTableModel) table.getModel(); for (i = table.getColumnCount(); i < count; ++i) { model.addColumn(""); minwidth[i] = minwidth[i - 1]; maxwidth[i] = maxwidth[i - 1]; prefwidth[i] = prefwidth[i - 1]; for (j = 0; j < table.getRowCount(); ++j) table.setValueAt("", j, i); } for (i = 0; i < count; ++i) { table.getColumnModel().getColumn(i).setMinWidth(minwidth[i]); table.getColumnModel().getColumn(i).setMaxWidth(maxwidth[i]); table.getColumnModel().getColumn(i).setPreferredWidth(prefwidth[i]); } } }