Java JTable Column Size Calculate calcColumnWidths(JTable table)

Here you can find the source of calcColumnWidths(JTable table)

Description

Setzt die Breite der TableColumns.

License

Open Source License

Parameter

Parameter Description
table a parameter

Declaration

public static void calcColumnWidths(JTable table) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.awt.Component;

import javax.swing.JTable;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;

public class Main {
    /**//from   www . j  av  a  2s  . co m
     * Setzt die Breite der TableColumns.
     * 
     * Quelle: http://www.chka.de/swing/table/cell-sizes.html
     * 
     * @param table
     */
    public static void calcColumnWidths(JTable table) {
        JTableHeader header = table.getTableHeader();

        TableCellRenderer defaultHeaderRenderer = null;

        if (header != null)
            defaultHeaderRenderer = header.getDefaultRenderer();

        TableColumnModel columns = table.getColumnModel();
        TableModel data = table.getModel();

        int margin = columns.getColumnMargin(); // only JDK1.3

        int rowCount = data.getRowCount();

        int totalWidth = 0;

        for (int i = columns.getColumnCount() - 1; i >= 0; --i) {
            TableColumn column = columns.getColumn(i);

            int columnIndex = column.getModelIndex();

            int width = -1;

            TableCellRenderer h = column.getHeaderRenderer();

            if (h == null)
                h = defaultHeaderRenderer;

            if (h != null) // Not explicitly impossible
            {
                Component c = h.getTableCellRendererComponent(table, column.getHeaderValue(), false, false, -1, i);

                width = c.getPreferredSize().width;
            }

            for (int row = rowCount - 1; row >= 0; --row) {
                TableCellRenderer r = table.getCellRenderer(row, i);

                Component c = r.getTableCellRendererComponent(table, data.getValueAt(row, columnIndex), false,
                        false, row, i);

                width = Math.max(width, c.getPreferredSize().width);
            }

            if (width >= 0)
                column.setPreferredWidth(width + margin); // <1.3: without margin
            else
                ; // ???

            totalWidth += column.getPreferredWidth();
        }

        //    only <1.3:   totalWidth += columns.getColumnCount() * columns.getColumnMargin();

        /* If you like; This does not make sense for two many columns!
        Dimension size = table.getPreferredScrollableViewportSize();
            
        size.width = totalWidth;
            
        table.setPreferredScrollableViewportSize(size);
        */

        // table.sizeColumnsToFit(-1); <1.3; possibly even table.revalidate()

        // if (header != null)
        //     header.repaint(); only makes sense when the header is visible (only <1.3)
    }
}

Related

  1. autoSizeTableColumns(JTable table)
  2. calcColumnWidth(JTable table, int col)
  3. calcColumnWidths(final JTable table)
  4. calcColumnWidths(final JTable table)
  5. calcColumnWidths(JTable table)
  6. calculateColumnWidth(JTable table, int columnIndex)
  7. calculateColumnWidth(JTable table, int columnIndex)
  8. calculateColumnWidth_1(JTable table, int pos)
  9. fixedColumSize(TableColumn c, int width)