Java JTable Column Width Set makeTableColumnWidthFit(JTable jTable, int col, int margin)

Here you can find the source of makeTableColumnWidthFit(JTable jTable, int col, int margin)

Description

make Table Column Width Fit

License

Open Source License

Declaration

public static void makeTableColumnWidthFit(JTable jTable, int col,
            int margin) 

Method Source Code

//package com.java2s;
/*/*from   w w  w .  j  av  a2  s. co m*/
 * JStock - Free Stock Market Software
 * Copyright (C) 2010 Yan Cheng CHEOK <yccheok@yahoo.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

import java.awt.*;

import javax.swing.*;
import javax.swing.table.*;

public class Main {
    public static void makeTableColumnWidthFit(JTable jTable, int col,
            int margin) {
        makeTableColumnWidthFit(jTable, col, margin, false);
    }

    public static void makeTableColumnWidthFit(JTable jTable, int col,
            int margin, boolean locking) {
        // strategy - get max width for cells in column and
        // make that the preferred width
        TableColumnModel columnModel = jTable.getColumnModel();
        int maxwidth = 0;

        for (int row = 0; row < jTable.getRowCount(); row++) {
            TableCellRenderer rend = jTable.getCellRenderer(row, col);
            Object value = jTable.getValueAt(row, col);
            Component comp = rend.getTableCellRendererComponent(jTable,
                    value, false, false, row, col);

            maxwidth = Math.max(comp.getPreferredSize().width + margin,
                    maxwidth);
        } // for row

        TableColumn column = columnModel.getColumn(col);
        TableCellRenderer headerRenderer = column.getHeaderRenderer();
        if (headerRenderer == null) {
            headerRenderer = jTable.getTableHeader().getDefaultRenderer();
        }
        Object headerValue = column.getHeaderValue();
        Component headerComp = headerRenderer
                .getTableCellRendererComponent(jTable, headerValue, false,
                        false, 0, col);

        maxwidth = Math.max(maxwidth, headerComp.getPreferredSize().width
                + margin);
        column.setPreferredWidth(maxwidth);
        if (locking) {
            // User will not able to adjust the width manually.
            column.setMinWidth(maxwidth);
            column.setMaxWidth(maxwidth);
        }
    }
}

Related

  1. getPreferredWidthForColumn(TableColumn col, JTable table)
  2. getPreferredWidthOfCell(JTable table, int rowIndex, int colIndex)
  3. getRenderedWidth(JTable table, int tableColumn, String value)
  4. getTotalColumnWidth(JTable table)
  5. initTableWidth(JTable table, int[] colWiths)
  6. setColumnMaxWidths(JTable tbl, Integer... widths)
  7. setColumnMinWidths(final JTable table)
  8. setColumnSize(TableColumnModel cm, int id, int percent, int tablesize, boolean resizeable)
  9. setColumnSizes(JTable table, double[] percentages)