Table Format Renderers
Posted by Rob Camick on October 11, 2008
JTable uses renderers to display data in each cell of the table. This allows for data to easily be displayed in different formats in any given cell. However, the table only provides support for a few basic renderers:
- Boolean – rendered with a check box.
- Number – rendered by a right-aligned label .
- Date – rendered by a label.
- Icon – rendered by a centered label.
- Object – rendered by a label that displays the object’s string value.
Not much to choose from when you want to jazz up the display of your data. Fortunately we can easily create a renderer which will allow us to format the data in various ways.
Most code examples of custom rendering show code that overrides the getTableCellRendererComponent(…) method to update the properties of the renderer directly. For simple formatting we will take a different approach, as we will be overriding the setValue(…) method. Using this approach we can take advantage of the Format class and the formatting capabilities of its sub classes, specifically the DateFormat and NumberFormat classes.
The FormatRenderer will require only a single parameter, the Format object to be used to format the data. For example:
SimpleDateFormat format = new SimpleDateFormat("yy/MM/dd");
TableCellRenderer renderer = new FormatRenderer( format );
A convenience class, NumberRenderer, was also created that simply uses right alignment for the renderer. In both classes, convenience static methods where created to return common renderers. The breakdown is as follows:
- FormatRenderer
- getDateTimeRenderer
- getTimeRenderer
- NumberRenderer
- getCurrencyRenderer
- getIntegerRenderer
- getPercentRenderer
The following code sample was used to generate the table pictured in the image:
TableColumnModel m = table.getColumnModel();
m.getColumn(0).setCellRenderer(FormatRenderer.getDateTimeRenderer());
m.getColumn(1).setCellRenderer(FormatRenderer.getTimeRenderer());
m.getColumn(2).setCellRenderer(NumberRenderer.getPercentRenderer());
m.getColumn(3).setCellRenderer(NumberRenderer.getCurrencyRenderer());
Get The Code
FormatRenderer.java
NumberRenderer.java

Alan Mehio said
Good site; very useful. Please keep on
Regards,
Alan Mehio
London, UK
Rob Camick said
Thanks. As long as I can think of something to write about I’ll keep going….
Marlo said
thank you for posting this very helpful tips, i solved a problem which i thought about a long time!
Rob Camick said
Glad the tip got you pointed in the right direction.
Bruce said
Thank you!!
It is great that you take the time to share your knowledge.
It is really helpful to see how a pro solves problems.
Rob Camick said
Thanks, but I wouldn’t call myself a pro. I just like to throw out ideas and let you decided if they make sense.
user said
Congratulations!
I tried in other ways with cellRenderers than not worked fine with numbers, only with dates.
Your sample helped me in both.
Thank you.
Rob Camick said
Glad you found a simple solution for both problems.
iron kettle said
Thanks so much for this code, going to try it now.
Sathish Robert said
Thank u
Amit said
Thanks a lot!! Your code sample was a godsend :-)
Rob Camick said
Thanks for the feedback.
FiruzzZ said
Very usefull stuff.. gracias
FiruzzZ said
I don’t know but maybe u..
Im using the NumberRenderer.getCurrencyRenderer() in 2 JTables, a Search of sales checks , them I double click and open other Dialog to see the details and FORMAT is DIFFERENT!
on seacher is : 1234.00
on details is : $1.234,00
Rob Camick said
It sounds like your seacher table is using the default renderer. Read the JTable API and follow the link to the Swing tutorial on “How to Use Tables” for more information about using custom renderers.
Rudy said
Wow, excellent code that you share, but how if the number format is in different format like our country :
1. The currency symbol is “Rp.”
2. The decimal separator is “,”.
3. The thousand separator is “.”.
4. The fraction digit is 2.
Thanks a lot before
Banoth said
Thanks a lot, excellent code, works perfect with the format in the country i live (netherlands)
Rob Camick said
Good to know the international formats work.