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. For example to create a simple date renderer you could do:
public class YMDRenderer extends DefaultTableCellRenderer { private Format formatter = new SimpleDateFormat("yy/MM/dd"); public void setValue(Object value) { // Format the Object before setting its value in the renderer try { if (value != null) value = formatter.format(value); } catch(IllegalArgumentException e) {} super.setValue(value); } }
However, after a while you may get tired of creating custom rendering classes so you can instead use the FormatRenderer class. This class will use the Format class and its subclasses, specifically the DateFormat and NumberFormat classes, to provide flexible formatting of your data. 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 have been 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.
Elliot said
I love it. Simple and effective. Thank you so much!
Roberto Silva said
This is code wonderful, high cohesion and very simple, thank you!
Michael Mossman said
I am trying to modify your Currency renderer to make it CENTER, but I am not having much luck. Can you please give me a pointer.
Thank you, Michael
Rob Camick said
You don’t need to modify any code. You just create an instance of the renderer and then change the alignment:
Then set the render for the column.
Michael Mossman said
Thank you for that help. The only thing that you might think obvious, but I learnt the hard way, is that this code must precede
` m.getColumn(3).setCellRenderer(center);`
Unfortunately, I had it the other way around, with setCellRenderer pointing at the default CurrencyRenderer and of course the centering never worked.
Thought I would just point this out in case there are any other learners out there.
Regards, Michael
Gjergji said
Please help me the code doesnt work in my project, i have formated the Date in database with the date/time and it doesnt work
Rob Camick said
Not sure what you are trying to do. You either create the renderer with a custom Format or you use the static method to get the default DateTime renderer, but you don’t use both. So if you just want a simple y/m/d format you use:
The TableModel must also contain a object, not a String object.
Anonymous said
No works with DefaultTableModel
tblProducto.getTableHeader().setFont(new Font(“SansSerif”, Font.BOLD, 12));
String iSql =”SELECT p.cod_pro, p.nom_pro, p.pre_pro,p.stock_pro, pr.nom_prov ”
+ “from producto p INNER JOIN proveedor pr ON (p.cod_prov = pr.cod_prov)”
+ “WHERE p.est_pro ='” + on + “‘ order by cod_pro asc”;
String[] rotulos = {“ID”,”Producto”,”S/. Precio”,”Stock”,”Proveedor”};
String [] Data = new String[5];
modelo = new DefaultTableModel(null,rotulos);
//tblusuario.getColumn(0).setPreferredWidth(100);
try{
Statement jSt = jcn.Con().createStatement();
ResultSet jRst = jSt.executeQuery(iSql);
//ResultSet jRst = jSt.executeQuery(iSql);
while(jRst.next()){
Data[0] = jRst.getString(“p.cod_pro”);
Data[1]= jRst.getString(“nom_pro”);
Data[2]= jRst.getObject(“pre_pro”);
Data[3]= jRst.getString(“stock_pro”);
Data[4]= jRst.getString(“nom_prov”);
modelo.addRow(Data);
}
txtSearch.setEnabled(true);
tblProducto.setModel(modelo);
Rob Camick said
1) The DefaultTableModel has nothing to do with the rendering of the data. All the TableModel does is provide the data for the renderer and all my tests were done using the DefaultTableModel, so I know the code works. 2) The code you posted has nothing to do with using one of the FormatRenderers so I’m not sure why you posted it.
Bruce Judd said
This is very nice work! I merged the 2 classes and the result is quite handy – thank you for pointing the way.
FREDY said
HOW CAN I REMOVE THAT DOLLAR SIGN FROM getCurrencyRenderer? PLEASE HELP COUZ I TRIED MANY TIMES WITH NO SUCCESS
Rob Camick said
Don’t use the default currency renderer. Instead you need to create your own custom Formatter. Also when using a forum don’t use upper case characters when asking a question. Learn by observing how other people use the website.
Anonymous said
Sorry for the rather basic question, but how do I change the date format to dd-MMM-YYYY ?
nick woodward said
Randomly clicked on this site and kept thinking, Rob Camick, I’ve seen that name before….
Ahh, CodeRanch, you’ve helped me several times before :D
Bookmarked!