Here you can find the source of resizeColumnWidth(JTable table)
Parameter | Description |
---|---|
table | a parameter |
public static void resizeColumnWidth(JTable table)
//package com.java2s; //License from project: Apache License import java.awt.Component; import javax.swing.JTable; import javax.swing.table.TableCellRenderer; import javax.swing.table.TableColumnModel; public class Main { /**/*from w w w. j a v a 2s .co m*/ * Method ini digunakan untuk mengatur lebar kolom tabel * agar otomatis menyesuaikan dengan isi pada kolom * tersebut * @param table */ public static void resizeColumnWidth(JTable table) { final TableColumnModel columnModel = table.getColumnModel(); for (int column = 0; column < table.getColumnCount(); column++) { int width = 50; // Min width 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); } } }