Java JTable Column Width Set getPreferredWidthOfCell(JTable table, int rowIndex, int colIndex)

Here you can find the source of getPreferredWidthOfCell(JTable table, int rowIndex, int colIndex)

Description

Returns the preferred width of a given cell in a table.

License

Apache License

Parameter

Parameter Description
table the table
colIndex the colum index
rowIndex the row index

Return

the preferred width of the cell

Declaration

public static int getPreferredWidthOfCell(JTable table, int rowIndex,
        int colIndex) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.awt.Component;

import javax.swing.JTable;

import javax.swing.table.TableCellRenderer;

public class Main {
    /**//from w  w  w.  j a  v a2 s  .c o m
     * Returns the preferred width of a given cell in a table.
     *
     * @param table the table
     * @param colIndex the colum index
     * @param rowIndex the row index
     * @return the preferred width of the cell
     */
    public static int getPreferredWidthOfCell(JTable table, int rowIndex,
            int colIndex) {

        int width = 0;

        // get width of column data
        TableCellRenderer renderer = table.getCellRenderer(rowIndex,
                colIndex);
        Component comp = renderer.getTableCellRendererComponent(table,
                table.getValueAt(rowIndex, colIndex), false, false,
                rowIndex, colIndex);
        width = Math.max(width, comp.getPreferredSize().width);

        return width;
    }
}

Related

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