adjust JTable Column Width - Java Swing

Java examples for Swing:JTable Column

Description

adjust JTable Column Width

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 {
    static public void adjustColWidth(JTable l_Table) {
        int i, width;
        TableColumn l_Col;/* ww  w . j  a va  2s.com*/

        for (i = 0; i < l_Table.getColumnCount(); i++) {
            l_Col = l_Table.getColumn(l_Table.getColumnName(i));
            width = getPreferredWidthForColumn(l_Table, l_Col) + 20;

            l_Col.setMinWidth(width);
            l_Col.setMaxWidth(width);

        }
    }

    static public void adjustColWidth(JTable l_Table, JTable slaveTable) {
        int i, width;
        TableColumn l_Col;

        for (i = 0; i < l_Table.getColumnCount(); i++) {
            l_Col = l_Table.getColumn(l_Table.getColumnName(i));
            width = getPreferredWidthForColumn(l_Table, l_Col) + 20;

            l_Col.setMinWidth(width);
            l_Col.setMaxWidth(width);

            TableColumn slaveColumn = slaveTable.getColumn(l_Table
                    .getColumnName(i));
            slaveColumn.setMinWidth(width);
            slaveColumn.setMaxWidth(width);
        }
    }

    static private int getPreferredWidthForColumn(JTable l_Table,
            TableColumn col) {
        int hw = columnHeaderWidth(l_Table, col);
        int cw = widestCellInColumn(l_Table, col);

        return hw > cw ? hw : cw;
    }

    static private int columnHeaderWidth(JTable l_Table, TableColumn col) {
        TableCellRenderer renderer = l_Table.getTableHeader()
                .getDefaultRenderer();

        Component comp = renderer.getTableCellRendererComponent(l_Table,
                col.getHeaderValue(), false, false, 0, 0);
        return comp.getPreferredSize().width;
    }

    static private int widestCellInColumn(JTable l_Table, TableColumn col) {
        int c = col.getModelIndex(), width = 0, maxw = 0;
        for (int r = 0; r < l_Table.getRowCount(); ++r) {
            TableCellRenderer renderer = l_Table.getCellRenderer(r, c);
            Component comp = renderer.getTableCellRendererComponent(
                    l_Table, l_Table.getValueAt(r, c), false, false, r, c);
            width = comp.getPreferredSize().width;
            maxw = width > maxw ? width : maxw;
        }

        return maxw;
    }
}

Related Tutorials