Java JTable Column Width Set getPreferredWidthForColumn(TableColumn col, JTable table)

Here you can find the source of getPreferredWidthForColumn(TableColumn col, JTable table)

Description

Takes a TableColumn and determines the preferred width for the entire column based on the contents of the row cells.

License

Open Source License

Parameter

Parameter Description
col The TableColumn object contianing the cells we're basing the size on.
table the JTable object that will contiain the given TableColumn. Used to get the renderer object.

Return

int

Declaration

public static int getPreferredWidthForColumn(TableColumn col,
        JTable table) 

Method Source Code

//package com.java2s;

import java.awt.*;

import javax.swing.*;

import javax.swing.table.*;

public class Main {
    /** Module name */
    private static final String MODULE_NAME = "FusionUtils.";

    /**/*from   w  w  w .j  a v  a2 s .  c  o m*/
     * Takes a TableColumn and determines the preferred width for the entire column based on the contents
     * of the row cells. Lifted from Graphic Java's chapter on Tables.
     *
     * @param col The TableColumn object contianing the cells we're basing the size on.
     * @param table the JTable object that will contiain the given TableColumn. Used to get the renderer object.
     *
     * @return int 
     */
    public static int getPreferredWidthForColumn(TableColumn col,
            JTable table) {
        String methodName = MODULE_NAME
                + "getPreferredWidthForColumn(TableColumn,JTable)";
        int retval = 0;

        int headerWidth = getColumnHeaderWidth(col, table);
        int cellWidth = getWidestCellInColumn(col, table);

        retval = headerWidth > cellWidth ? headerWidth : cellWidth;

        //retval += 20; //JGD Fudge Factor

        //Logger.log( methodName + " retval: " + retval, Logger.INFO );

        return retval;
    }

    /**
     * Figures out the width of the column header for the given TableColumn.
     * Lifted from Graphic Java's chapter on Tables.
     *
     * @param col The TableColumn object contianing the header we're basing the size on.
     * @param table the JTable object that will contiain the given TableColumn. Used to get the renderer object.
     *
     * @return int
     */
    private static int getColumnHeaderWidth(TableColumn col, JTable table) {
        String methodName = MODULE_NAME
                + "getColumnHeaderWidth(TableColumn.JTable)";
        int retval = -1;

        //JGD THis seems to be returning null a lot. Could my friends at Graphic Java be lying to me???
        //JGD Yes, they were. The default value of TableColumn.headerRenderer is null. If it's null, it 
        //JGD uses the defaultHeaderRenderer which is just a JLabel (See TableColumn.java) 3/19/03
        TableCellRenderer renderer = col.getHeaderRenderer();

        if (renderer != null) {
            Component comp = renderer.getTableCellRendererComponent(table,
                    col.getHeaderValue(), false, false, 0, 0);
            retval = comp.getPreferredSize().width;
        } else //renderer is null, the default is a label, create our own label to figure it out
        {
            JLabel label = new JLabel((String) col.getHeaderValue(),
                    SwingConstants.CENTER);
            retval = label.getPreferredSize().width;
        }

        //Logger.log( methodName + " retval: " + retval, Logger.INFO );
        return retval;
    }

    /**
     * Figures out the width of the widest cell in a TableColumn
     * Lifted from Graphic Java's chapter on Tables.
     *
     * @param col The TableColumn object contianing the cells we're basing the size on.
     * @param table the JTable object that will contiain the given TableColumn. Used to get the renderer object.
     *
     * @return int
     */
    private static int getWidestCellInColumn(TableColumn col, JTable table) {
        String methodName = MODULE_NAME
                + "getWidestCellInColumn(TableColumn, JTable)";
        int retval = -1;
        int modelIndex = col.getModelIndex();
        int width = 0;
        int maxWidth = 0;

        for (int i = 0; i < table.getRowCount(); i++) {
            TableCellRenderer renderer = table.getCellRenderer(i,
                    modelIndex);
            Component comp = renderer.getTableCellRendererComponent(table,
                    table.getValueAt(i, modelIndex), false, false, i,
                    modelIndex);
            width = comp.getPreferredSize().width;
            maxWidth = width > maxWidth ? width : maxWidth;
        }
        //JGD Fudge it just a little...
        retval = maxWidth + 5;

        //Logger.log( methodName + " retval: " + retval, Logger.INFO );

        return retval;
    }
}

Related

  1. calcColumnWidth(int col)
  2. chooseGoodColumnWidths(JTable table)
  3. fitColumnWidth(JTable table, int colIndex, int padding)
  4. fitColumnWidths(JTable table, int padding)
  5. fitColumnWidths(TableModel model, JTable mainTable)
  6. getPreferredWidthOfCell(JTable table, int rowIndex, int colIndex)
  7. getRenderedWidth(JTable table, int tableColumn, String value)
  8. getTotalColumnWidth(JTable table)
  9. initTableWidth(JTable table, int[] colWiths)