Java JTable Column Pack packColumn(JTable table, int vColIndex, int margin)

Here you can find the source of packColumn(JTable table, int vColIndex, int margin)

Description

PackColumn sets the preferred width of the visible column specified by vColIndex.

License

Apache License

Parameter

Parameter Description
table The table you want to resize a column.
vColIndex The column number.
margin Extra spaces for each side of column.

Declaration

public static void packColumn(JTable table, int vColIndex, int margin) 

Method Source Code

//package com.java2s;
/**//from  w  w  w. j  a  v a2s  .  c  o  m
 * Copyright 2008 WebPhotos
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import javax.swing.JTable;

import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.TableColumn;

public class Main {
    /**
     * PackColumn sets the preferred width of the visible column specified by
     * vColIndex. The column will be just wide enough to show the column head
     * and the widest cell in the column. margin pixels are added to the left
     * and right (resulting in an additional width of 2*margin pixels).
     *
     * @param table The table you want to resize a column.
     * @param vColIndex The column number.
     * @param margin Extra spaces for each side of column.
     */
    public static void packColumn(JTable table, int vColIndex, int margin) {
        DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel();
        TableColumn col = colModel.getColumn(vColIndex);
        int width;

        // Get width of column header
        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,
                0, 0);
        width = comp.getPreferredSize().width;

        // Get maximum width of column data
        for (int r = 0; r < table.getRowCount(); r++) {
            renderer = table.getCellRenderer(r, vColIndex);
            comp = renderer.getTableCellRendererComponent(table, table.getValueAt(r, vColIndex), false, false, r,
                    vColIndex);
            width = Math.max(width, comp.getPreferredSize().width);
        }

        // Add margin
        width += 2 * margin;

        // Set the width
        col.setPreferredWidth(width);
    }
}

Related

  1. packColumn(JTable table, int colIndex)
  2. packColumn(JTable table, int colIndex)
  3. packColumn(JTable table, int vColIndex, int margin)
  4. packColumn(JTable table, int vColIndex, int margin)
  5. packColumn(JTable table, int vColIndex, int margin)
  6. packColumns(final JTable table, final int margin)
  7. packColumns(JTable table)
  8. packColumns(JTable table, int margin)
  9. packColumns(JTable table, int width)