Setting column width and row height for JTable
- Details
- Written by Nam Ha Minh
- Last Updated on 06 July 2019   |   Print Email
When working with JTable in Swing, sometimes we need the table to have columns in different widths and rows in different heights, other than the defaults provided by the JTable component.
Let’s see how to change the defaults of column width and row height of this sample Swing program:
1. Setting row height for JTable:
This can be done easily using these two methods of the JTable class:
- setRowHeight(int row, int rowHeight): sets the height (in pixels) for an individual row.
- setRowHeight(int rowHeight): sets the height (in pixels) for all rows in the table and discards heights of all rows were set individually before.
For example, the following statement set the height of all rows to 30 pixels:
table.setRowHeight(30);
Result:
Interestingly, we can set height for an individual row afterward, for example:
table.setRowHeight(30); table.setRowHeight(3, 60);
That set height for all rows to 30 pixels, except the 4th row whose height is set to 60 pixels. Result:
2. Setting column width for JTable
This seems to be as easy as setting row height, but it is actually not. The following attempt will fail:
TableColumnModel columnModel = table.getColumnModel(); columnModel.getColumn(0).setWidth(100);
It’s because the JTable doesn’t respect the width value of a column. Instead, it is more interested in the column’s preferred width. Here’s the second attempt:
columnModel.getColumn(0).setPreferredWidth(100);
However, setting preferred width for only column doesn’t work. The table needs to know the preferred width of all columns, thus the following statements set preferred width for all of 4 columns of the table:
columnModel.getColumn(0).setPreferredWidth(50); columnModel.getColumn(1).setPreferredWidth(150); columnModel.getColumn(2).setPreferredWidth(100); columnModel.getColumn(3).setPreferredWidth(180);
Then the result looks pretty nice:
NOTE: It’s recommended to set the preferred width of all columns in a way that the total width is approximately equal to the preferred with of the table (or a JScrollPane because a JTable is usually placed inside a JScrollPane). For example, the following statements set preferred size for the enclosing scroll pane:
JScrollPane scrollpane = new JScrollPane(table); scrollpane.setPreferredSize(new Dimension(480, 300));
3. A utility method for setting column width for JTable
For convenience, we can create a utility method for setting the width of columns in a table for the sake of reusability. For example:
public static void setJTableColumnsWidth(JTable table, int tablePreferredWidth, double... percentages) { double total = 0; for (int i = 0; i < table.getColumnModel().getColumnCount(); i++) { total += percentages[i]; } for (int i = 0; i < table.getColumnModel().getColumnCount(); i++) { TableColumn column = table.getColumnModel().getColumn(i); column.setPreferredWidth((int) (tablePreferredWidth * (percentages[i] / total))); } }
And use this method as follows:
setJTableColumnsWidth(table, 480, 10, 30, 30, 30);
Note, instead of setting absolute pixels for each column, we specify each column width in percentage of the given preferred table width, and the total should make 100% percentage.
You can download the source code and executable JAR file of the demo program in the attachments section. The final program looks like this:
References:
Related JTable Tutorials:
- Java Swing JTable Simple Example
- Editable JTable Example
- JTable Simple Renderer Example
- JTable column header custom renderer examples
- 6 Techniques for Sorting JTable You Should Know
- How to scroll JTable row to visible area programmatically
- JTable popup menu example
- How to handle mouse clicking event on JTable column header
- How to create JComboBox cell editor for JTable
Comments
If I want to set column widths only once at the time of table being shown and then later on allow user to resize columns then how I can do that?
this code useful me.
thanks for sharing!!!