Here you can find the source of packTables(final JTable tableOne, final JTable tableTwo)
Parameter | Description |
---|---|
tableOne | table 1 |
tableTwo | table 2 |
public static void packTables(final JTable tableOne, final JTable tableTwo)
//package com.java2s; /*/* www. j a va 2 s . c o m*/ * jGnash, a personal finance application * Copyright (C) 2001-2017 Craig Cavanaugh * * 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 3 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, see <http://www.gnu.org/licenses/>. */ import java.awt.Component; import javax.swing.JTable; import javax.swing.table.TableCellRenderer; import javax.swing.table.TableColumn; import javax.swing.table.TableColumnModel; public class Main { /** * Pads the minimum calculated with of the column. Depending on the platform * and the fonts used, java does not always return a good calculated minimum * width */ private static final int MIN_WIDTH_PADDING = 15; /** * Packs two table and forces them to have the same column width. * Assumes both tables have the same number of columns * * @param tableOne table 1 * @param tableTwo table 2 */ public static void packTables(final JTable tableOne, final JTable tableTwo) { packGenericTable(tableOne); packGenericTable(tableTwo); for (int i = 0; i < tableOne.getModel().getColumnCount(); i++) { int tableWidth = tableOne.getColumnModel().getColumn(i) .getPreferredWidth(); int footerTableWidth = tableTwo.getColumnModel().getColumn(i) .getPreferredWidth(); if (tableWidth > footerTableWidth) { tableTwo.getColumnModel().getColumn(i) .setPreferredWidth(tableWidth); } else { tableOne.getColumnModel().getColumn(i) .setPreferredWidth(footerTableWidth); } } } /** * Sizes the columns in a JTable * * @param table The table to size the columns in */ public static void packGenericTable(final JTable table) { TableColumnModel model = table.getColumnModel(); int columns = model.getColumnCount(); for (int i = 0; i < columns; i++) { TableColumn col = model.getColumn(i); // Get the column header width TableCellRenderer renderer = col.getHeaderRenderer(); if (renderer == null) { renderer = table.getTableHeader().getDefaultRenderer(); } Component comp = renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false, false, 0, 0); int width = comp.getPreferredSize().width; // Find the largest width in the column int rows = table.getRowCount(); for (int r = 0; r < rows; r++) { renderer = table.getCellRenderer(r, i); comp = renderer.getTableCellRendererComponent(table, table.getValueAt(r, i), false, false, r, i); width = Math.max(width, comp.getPreferredSize().width); } // Set the column width col.setPreferredWidth(width + MIN_WIDTH_PADDING); } } }