calculate JavaFX Column Width - Java JavaFX

Java examples for JavaFX:Table

Description

calculate JavaFX Column Width

Demo Code


//package com.java2s;
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;

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.  j  ava 2 s  .c om
            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 Tutorials