Java Utililty Methods JTable Column Pack

List of utility methods to do JTable Column Pack

Description

The list of methods to do JTable Column Pack are organized into topic(s).

Method

voidpackColumn(JTable table, int colIndex)
Sets the preferred width of a column of a JTable specified by colIndex.
DefaultTableColumnModel columnModel = (DefaultTableColumnModel) table.getColumnModel();
TableColumn column = columnModel.getColumn(colIndex);
int width;
TableCellRenderer renderer = column.getHeaderRenderer();
if (renderer == null) {
    renderer = table.getTableHeader().getDefaultRenderer();
Component component = renderer.getTableCellRendererComponent(table, column.getHeaderValue(), false, false,
...
voidpackColumn(JTable table, int colIndex)
pack Column
TableColumnModel colModel = table.getColumnModel();
TableColumn col = colModel.getColumn(colIndex);
int width = 0;
TableCellRenderer renderer = col.getHeaderRenderer();
if (renderer == null) {
    renderer = table.getTableHeader().getDefaultRenderer();
Component comp = renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false, false, 0, 0);
...
voidpackColumn(JTable table, int vColIndex, int margin)
Sets the preferred width of the visible column specified by vColIndex.
DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel();
TableColumn col = colModel.getColumn(vColIndex);
int width = 0;
TableCellRenderer renderer = col.getHeaderRenderer();
if (renderer == null) {
    renderer = table.getTableHeader().getDefaultRenderer();
Component comp = renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false, false, 0, 0);
...
voidpackColumn(JTable table, int vColIndex, int margin)
Sets the preferred width of the visible column specified by vColIndex.
TableColumnModel colModel = table.getColumnModel();
TableColumn col = colModel.getColumn(vColIndex);
int width = 0;
TableCellRenderer renderer = col.getHeaderRenderer();
if (renderer == null) {
    renderer = table.getTableHeader().getDefaultRenderer();
Component comp = renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false, false, 0, 0);
...
voidpackColumn(JTable table, int vColIndex, int margin)
PackColumn sets the preferred width of the visible column specified by vColIndex.
DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel();
TableColumn col = colModel.getColumn(vColIndex);
int width;
javax.swing.table.TableCellRenderer renderer = col.getHeaderRenderer();
if (renderer == null) {
    renderer = table.getTableHeader().getDefaultRenderer();
java.awt.Component comp = renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false, false,
...
voidpackColumn(JTable table, int vColIndex, int margin)
pack Column
DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel();
TableColumn col = colModel.getColumn(vColIndex);
int width = 0;
TableCellRenderer renderer = col.getHeaderRenderer();
if (renderer == null) {
    renderer = table.getTableHeader().getDefaultRenderer();
Component comp = renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false, false, 0, 0);
...
voidpackColumns(final JTable table, final int margin)
pack Columns
for (int index = 0; index < table.getColumnCount(); index++) {
    packColumn(table, index, margin);
voidpackColumns(JTable table)
Adjusts the widths of columns to be just wide enough to show all of the column headers and the widest cells in the columns
for (int columnIndex = 0; columnIndex < table.getColumnCount(); columnIndex++) {
    packColumn(table, columnIndex, 2);
voidpackColumns(JTable table, int margin)
pack Columns
for (int c = 0; c < table.getColumnCount(); c++) {
    packColumn(table, c, 2);
voidpackColumns(JTable table, int width)
pack Columns
int padding = 5;
TableColumnModel columnModel = table.getColumnModel();
int[] widths = new int[columnModel.getColumnCount()];
for (int j = 0; j < table.getRowCount(); j++) {
    for (int i = 0; i < widths.length; i++) {
        TableCellRenderer renderer = table.getCellRenderer(j, i);
        Object value = table.getValueAt(j, i);
        Component component = renderer.getTableCellRendererComponent(table, value, true, true, j, i);
...