Java Utililty Methods JTable Column Resize

List of utility methods to do JTable Column Resize

Description

The list of methods to do JTable Column Resize are organized into topic(s).

Method

voidreSizeColumn(TableColumn col, int min, int preferred, int max)
re Size Column
col.setMinWidth(min);
col.setPreferredWidth(preferred);
col.setMaxWidth(max);
voidresizeColumns(JTable table)
Resize all columns in the table to fit widest row including header.
if (table.getGraphics() == null) {
    return;
DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
FontMetrics fm = table.getFontMetrics(renderer.getFont());
TableColumnModel mod = table.getColumnModel();
for (int iCol = 0; iCol < mod.getColumnCount(); iCol++) {
    TableColumn col = mod.getColumn(iCol);
...
voidresizeColumnWidth(JTable table)
Method ini digunakan untuk mengatur lebar kolom tabel agar otomatis menyesuaikan dengan isi pada kolom tersebut
final TableColumnModel columnModel = table.getColumnModel();
for (int column = 0; column < table.getColumnCount(); column++) {
    int width = 50; 
    for (int row = 0; row < table.getRowCount(); row++) {
        TableCellRenderer renderer = table.getCellRenderer(row, column);
        Component comp = table.prepareRenderer(renderer, row, column);
        width = Math.max(comp.getPreferredSize().width + 1, width);
    columnModel.getColumn(column).setPreferredWidth(width);