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:

JTable in default column width and row height

 

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:

JTable set height for all rows

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:

JTable set height for an individual row

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:

JTable different column widths

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:

JTable custom column width and row height

 

References:

 

Related JTable Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.



Attachments:
Download this file (JTableColumnWidthRowHeightExample.zip)JTableColumnWidthRowHeightExample.zip[Source code and executable JAR file]9 kB

Add comment

   


Comments 

#8Oshadha Induwara2021-06-20 05:54
Thank you very much. well explained.
Quote
#7Rupesh2020-10-12 05:17
After setting preferredWidth() for all columns, user is not able to resize columns by dragging.
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?
Quote
#6Gbenga2020-04-17 06:27
Thanks, useful coode
Quote
#5vidarsha2019-10-21 08:36
thank you.
this code useful me.
Quote
#4thomas2018-05-23 13:01
nice utility to set the width of my table.
thanks for sharing!!!
Quote