Java JTable Column Pack packColumnsWidthFixedFirst(JTable table, int margin)

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

Description

pack Columns Width Fixed First

License

Open Source License

Declaration

public static int packColumnsWidthFixedFirst(JTable table, int margin) 

Method Source Code


//package com.java2s;
/*/*from  www .ja v a 2 s  .  c o  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 packColumnsWidthFixedFirst(JTable table, int margin) {
        int width = 0;
        for (int c = 0; c < table.getColumnCount(); c++) {
            if (c == 0) {
                width += packColumn(table, c, margin, true);
            } else {
                width += packColumn(table, c, margin, false);
            }
        }
        return width;
    }

    public static int packColumn(JTable table, int vColIndex, int margin, boolean fixed) {
        //TableModel model = table.getModel();
        DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel();
        if (vColIndex < table.getColumnCount()) {
            TableColumn col = colModel.getColumn(vColIndex);
            if (col != null) {
                if (fixed) {
                    col.setResizable(false);
                    int width = getPreferredColumnWidth(table, vColIndex, margin);
                    if (width != col.getPreferredWidth()) {
                        //System.out.println("Setting first column width to: " + width);
                        col.setMinWidth(width);
                        col.setMaxWidth(width);
                        col.setPreferredWidth(width);
                    }
                    return width;
                } else {
                    int width = getPreferredColumnWidth(table, vColIndex, margin);
                    col.setPreferredWidth(width);
                    return width;
                }
            }
        }
        return 0;
    }

    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. packColumn(JTable table, int vColIndex, int margin)
  2. packColumns(final JTable table, final int margin)
  3. packColumns(JTable table)
  4. packColumns(JTable table, int margin)
  5. packColumns(JTable table, int width)