Java JTable Cell getWidestCellInColumn(TableColumn col, JTable table)

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

Description

Figures out the width of the widest cell in a TableColumn Lifted from Graphic Java's chapter on Tables.

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

private static int getWidestCellInColumn(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.";

    /**/* w ww  .  j  a  v  a2 s.co  m*/
     * 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. getContent(JTable table, String lineBreak, String cellBreak, int columnCount, int rowCount, int[] selectedRowsCount, int[] selectedColumsCount)
  2. getCurrentSelectionContent(JTable table, String lineBreak, String cellBreak)
  3. getIntercellWidth(JTable table)
  4. getTableFocusCellForeground()
  5. getWidestCellInColumn(JTable table, TableColumn col)
  6. isCellVisible(JTable table, int rowIndex, int vColIndex)
  7. renderHTMLCell(final StringBuffer buffer, final String contents, final String css, final int align)
  8. setCellTextAllignment(JTable table, int alignment)
  9. showCell(JTable table, int row, int column)