widest JTable Cell In Column - Java Swing

Java examples for Swing:JTable Column

Description

widest JTable Cell In Column

Demo Code


//package com.java2s;
import java.awt.Component;

import javax.swing.JTable;

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

public class Main {
    private static int widestCellInColumn(JTable table, TableColumn col) {
        int c = col.getModelIndex(), width = 0, maxw = 0;
        for (int r = 0; r < table.getRowCount(); ++r) {
            TableCellRenderer renderer = table.getCellRenderer(r, c);
            Component comp = renderer.getTableCellRendererComponent(table,
                    table.getValueAt(r, c), false, false, r, c);
            width = comp.getPreferredSize().width + 2;
            maxw = width > maxw ? width : maxw;
        }/*from www  . ja  va  2  s .  co  m*/
        return maxw;
    }
}

Related Tutorials