Java JTable Cell getWidestCellInColumn(JTable table, TableColumn col)

Here you can find the source of getWidestCellInColumn(JTable table, TableColumn col)

Description

get Widest Cell In Column

License

Open Source License

Declaration

public static int getWidestCellInColumn(JTable table, TableColumn col) 

Method Source Code


//package com.java2s;
/* This file is a part of the sqlHawk project.
 * http://timabell.github.com/sqlHawk//* ww  w  .ja  v a2  s . c  o  m*/
 *
 *  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 3 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, see <http://www.gnu.org/licenses/>. */

import java.awt.Component;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;

public class Main {
    public static int getWidestCellInColumn(JTable table, TableColumn col) {
        int column = col.getModelIndex();
        int max = 0;

        for (int row = 0; row < table.getRowCount(); ++row) {
            TableCellRenderer renderer = table.getCellRenderer(row, column);
            Component comp = renderer.getTableCellRendererComponent(table, table.getValueAt(row, column), false,
                    false, row, column);
            max = Math.max(comp.getPreferredSize().width, max);
        }

        return max;
    }
}

Related

  1. getColumnNames(JTable table, String lineBreak, String cellBreak)
  2. getContent(JTable table, String lineBreak, String cellBreak, int columnCount, int rowCount, int[] selectedRowsCount, int[] selectedColumsCount)
  3. getCurrentSelectionContent(JTable table, String lineBreak, String cellBreak)
  4. getIntercellWidth(JTable table)
  5. getTableFocusCellForeground()
  6. getWidestCellInColumn(TableColumn col, JTable table)
  7. isCellVisible(JTable table, int rowIndex, int vColIndex)
  8. renderHTMLCell(final StringBuffer buffer, final String contents, final String css, final int align)
  9. setCellTextAllignment(JTable table, int alignment)