Packing a JTable Component

This example demonstrates how to adjust the preferred size of a JTable to be just large enough to accommodate the preferred size of all cells.
int rows = 10; int cols = 5; JTable table = new JTable(rows, cols) { // Override this method so that it returns the preferred // size of the JTable instead of the default fixed size public Dimension getPreferredScrollableViewportSize() { return getPreferredSize(); } }; // Allow columns to be resized table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); // Add data... // These packing methods are defined in Packing a Column of a JTable Component // and Setting the Height of a Row in a JTable Component packColumns(table, 2); packRows(table, 0);

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.