Java JTable Column Size Calculate calculateColumnWidth(JTable table, int columnIndex)

Here you can find the source of calculateColumnWidth(JTable table, int columnIndex)

Description

calculate Column Width

License

Open Source License

Declaration

public static int calculateColumnWidth(JTable table, int columnIndex) 

Method Source Code

//package com.java2s;

import java.awt.Component;

import javax.swing.JTable;

import javax.swing.table.TableCellRenderer;

public class Main {

    public static int calculateColumnWidth(JTable table, int columnIndex) {
        int width = 0; // The return value
        int rowCount = table.getRowCount();
        for (int i = 0; i < rowCount; i++) {
            TableCellRenderer renderer = table.getCellRenderer(i,
                    columnIndex);//from   w w w.  ja  v  a 2s .com
            Component comp = renderer.getTableCellRendererComponent(table,
                    table.getValueAt(i, columnIndex), false, false, i,
                    columnIndex);
            int thisWidth = comp.getPreferredSize().width;
            if (thisWidth > width) {
                width = thisWidth;
            }
        }
        return width;
    }
}

Related

  1. calcColumnWidths(final JTable table)
  2. calcColumnWidths(final JTable table)
  3. calcColumnWidths(JTable table)
  4. calcColumnWidths(JTable table)
  5. calculateColumnWidth(JTable table, int columnIndex)
  6. calculateColumnWidth_1(JTable table, int pos)
  7. fixedColumSize(TableColumn c, int width)
  8. fixSize(TableColumn column, JTable table)
  9. initColumnSizes(JTable table)