How to specify JTable's column widths

How to specify JTable's column widths

Written by Don Young , InPowerSoft,  February 2006

Introduction

Very often, when you construct a JTable and bind it to a ADF view object, many attributes of view object will be displayed in columns of a JTable. If you construct a JTable with automatic column sizing, then when the JTable is resized, its columns will also be resized accordingly. But how would you control the relative width proportion of each column within a JTable. This article shows a simple API to achieve this effect. With this function, you can limit the relative width of each column in proportion to other columns and this proportion is kept automatically even during the JTable resizing process.

Step #A

Ideally, we like a function such that we can do the followings:

 
18       DbTable jobTable = new DbTable(getFormParameters()); 19    
20       jobTable.createBinding(block, new String[]{"JobTitle", "MinSalary", "MaxSalary"}); 
21    
22       jobTable.setPreferredColumnWidths(new double[]{0.4, 0.2, 0.2});

Line 18 creates a JTable in this case is a derived version called DbTable. We specify in line 20 the column bindings to the view object. Line 22 is the API of interest and we specify their relative widths. The source code is taken from the demo example at the InPowerSoft download page. The end result will look like the following:

 

Step #B

Let's dissect the function we will be building:

134    public void setPreferredColumnWidths(double[] percentages) 
135    { 
136      Dimension tableDim = this.getPreferredSize(); 
137   
138      double total = 0; 
139      for(int i = 0; i < getColumnModel().getColumnCount(); i++) 
140        total += percentages[i]; 
141   
142   
143      for(int i = 0; i < getColumnModel().getColumnCount(); i++) 
144      { 
145        TableColumn column = getColumnModel().getColumn(i); 
146        column.setPreferredWidth((int) (tableDim.width * (percentages[i] / total))); 
147      } 
148    }

This function cannot be more simpler. The principle is to set the preferred size at each column according to the relative proportion specified at the percentages array passed in. Line 136 give us the preferred size of the table as it is determined by the layout manager of this JTable. Line 138 to 140 give us the sum of each column's proportional value. We then go through another pass and compute each column's relative proportion and use the table's preferred width to set each column's preferred width. Note, the value in percentages array does not have to add up to 1.0. As a matter of fact, you can specify an array of any floating point number and this function will work properly. This feature is handy because many often you will find yourself adding and deleting columns as a frequent exercise during the coding process and if you have to calibrate the sum to 1.0, it will be a bit tedious.

Step #C

One obvious improvement can be made to this function. We do not test for negative floating point values. Instead of disallowing negative floating point values, we may use negative floating point values to specify the exact number of pixels a column can paint instead of its relative width in relation to others. This is left as an exercise to your imagination.

You must not call this function before you create your columns or before create binding to the view object that creates the columns. Note also by setting the preferred size on the columns, you only specify the preference of column width but the layout managers will determine how is best to show each column according to a variety of other factors. Generally, you will get very close to what you want.

 

Summary

In this article, we show a relative simple but handy function to specify the column widths on a Swing JTable. I found it to be very useful throughout application I wrote as data come with different length. Hope you will find it useful as well for your project.

 

E-mail this page
Printer View Printer View
Oracle Is The Information Company About Oracle | Oracle RSS Feeds | Careers | Contact Us | Site Maps | Legal Notices | Terms of Use | Privacy