Java JTable Column Width Set autoResizeColWidth(final JTable table, final DefaultTableModel model)

Here you can find the source of autoResizeColWidth(final JTable table, final DefaultTableModel model)

Description

auto Resize Col Width

License

Open Source License

Parameter

Parameter Description
table a parameter
model a parameter

Declaration

public static JTable autoResizeColWidth(final JTable table, final DefaultTableModel model) 

Method Source Code

//package com.java2s;
/* Copyright (C) 2015, University of Kansas Center for Research
 * /* w  ww  . j  a va2  s .c om*/
 * Specify Software Project, specify@ku.edu, Biodiversity Institute,
 * 1345 Jayhawk Boulevard, Lawrence, Kansas, 66045, USA
 * 
 * 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 java.awt.Component;

import javax.swing.JTable;

import javax.swing.SwingConstants;

import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.DefaultTableModel;

import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;

public class Main {
    /**
     * @param table
     * @param model
     * @return
     */
    public static JTable autoResizeColWidth(final JTable table, final DefaultTableModel model) {
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        table.setModel(model);

        int margin = 5;

        DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel();

        int preferredWidthTotal = 0;
        int renderedWidthTotal = 0;
        int[] colWidths = new int[table.getColumnCount()];
        for (int i = 0; i < table.getColumnCount(); i++) {
            int vColIndex = i;
            TableColumn col = colModel.getColumn(vColIndex);
            int width = 0;

            // Get width of column header
            TableCellRenderer renderer = col.getHeaderRenderer();

            if (renderer == null) {
                renderer = table.getTableHeader().getDefaultRenderer();
            }

            Component comp = renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false, false, 0,
                    0);

            width = comp.getPreferredSize().width;

            // Get maximum width of column data
            for (int r = 0; r < table.getRowCount(); r++) {
                renderer = table.getCellRenderer(r, vColIndex);
                comp = renderer.getTableCellRendererComponent(table, table.getValueAt(r, vColIndex), false, false,
                        r, vColIndex);
                width = Math.max(width, comp.getPreferredSize().width);
            }

            // Add margin
            width += 2 * margin;

            preferredWidthTotal += col.getPreferredWidth();
            colWidths[i] = width;

            renderedWidthTotal += width;
        }

        if (renderedWidthTotal > preferredWidthTotal) {
            for (int i = 0; i < table.getColumnCount(); i++) {
                colModel.getColumn(i).setPreferredWidth(colWidths[i]);
            }
        }

        ((DefaultTableCellRenderer) table.getTableHeader().getDefaultRenderer())
                .setHorizontalAlignment(SwingConstants.LEFT);

        // table.setAutoCreateRowSorter(true);
        table.getTableHeader().setReorderingAllowed(false);

        return table;
    }
}

Related

  1. adjustColumnWidth(JTable table, int buf)
  2. adjustColumnWidth(JTable tbl, int col, int maxColumnWidth)
  3. adjustColumnWidth(TableModel model, int columnIndex, int maxWidth, int rightPadding, JTable table)
  4. adjustColWidth(JTable table, int firstColumnWidth, int middleColumnWidth, int lastColumnWidth, int padding)
  5. autoFitColumnWidth(JTable table, TableColumn tableColumn)
  6. autoResizeColWidth(JTable table, AbstractTableModel model)
  7. autoResizeColWidth(JTable table, int extraSpaceToColumn)
  8. autoResizeColWidthNoFill(JTable table)
  9. calcColumnWidth(int col)