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:
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:
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)));
}
}
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:
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.
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?
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!!!