Java JTable Column Width Get getPreferredColumnWidth(JTable table, int vColIndex, int margin)

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

Description

get Preferred Column Width

License

Open Source License

Declaration

public static int getPreferredColumnWidth(JTable table, int vColIndex, int margin) 

Method Source Code


//package com.java2s;
/*/*from   w w  w .  ja v  a2  s  . co m*/
 * Copyright (c) 2009 Albert Kurucz. 
 *
 * This file, Util.java is part of JTStand.
 *
 * JTStand is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * JTStand is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with GTStand.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.awt.Component;

import java.awt.Dimension;

import javax.swing.JTable;

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

public class Main {
    public static int getPreferredColumnWidth(JTable table, int vColIndex, int margin) {
        int width = 0;
        if (vColIndex >= table.getColumnCount()) {
            return 0;
        }
        DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel();
        TableColumn col = colModel.getColumn(vColIndex);

        // Get width of column header
        TableCellRenderer renderer = col.getHeaderRenderer();
        if (renderer == null) {
            renderer = table.getTableHeader().getDefaultRenderer();
        }
        if (renderer != null) {
            Component c = renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false, false, 0, 0);
            if (c != null) {
                Dimension d = c.getPreferredSize();
                if (d != null) {
                    width = d.width;
                }
            }
        }

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

        // Add margin
        width += 2 * margin;
        return width;
    }
}

Related

  1. getColumnWidth(JTable table)
  2. getColumnWidths(final JTable table)
  3. getColumnWidths(JTable p_Table)
  4. getColumnWidths(JTable tbl)
  5. getPreferredColumnWidth(JTable table, TableColumn col)
  6. getPreferredHeaderWidth(JTable tbl, int iColumn)