Java JTable Column Width Set autoResizeColWidth(JTable table, AbstractTableModel model)

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

Description

Resizes the table columns based on the column and data preferred widths.

License

LGPL

Parameter

Parameter Description
table the original table
model the data model

Return

the table itself

Declaration

public static JTable autoResizeColWidth(JTable table, AbstractTableModel model) 

Method Source Code


//package com.java2s;
/*/* www .ja va  2s. co m*/
 * Copyright 2008-2014, David Karnok 
 * The file is part of the Open Imperium Galactica project.
 * 
 * The code should be distributed under the LGPL license.
 * See http://www.gnu.org/licenses/lgpl.html for details.
 */

import java.awt.Component;

import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;

public class Main {
    /**
     * Resizes the table columns based on the column and data preferred widths.
     * @param table the original table
     * @param model the data model
     * @return the table itself
     */
    public static JTable autoResizeColWidth(JTable table, AbstractTableModel model) {
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        table.setModel(model);

        int margin = 5;

        for (int i = 0; i < table.getColumnCount(); i++) {
            int vColIndex = i;
            DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel();
            TableColumn col = colModel.getColumn(vColIndex);
            int width;

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

            // Set the width
            col.setPreferredWidth(width);
        }

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

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

        //        for (int i = 0; i < table.getColumnCount(); i++) {
        //            TableColumn column = table.getColumnModel().getColumn(i);
        // 
        //            column.setCellRenderer(new DefaultTableColour());
        //        }

        return table;
    }
}

Related

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