Java Tips Weblog

  • Blog Stats

    • 1,257,874 hits
  • Categories

  • Archives

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

Related Reading

Concepts: Editor and Renderers
Java API: java.text.Format

About these ads

30 Responses to “Table Format Renderers”

  1. Alan Mehio said

    Good site; very useful. Please keep on

    Regards,
    Alan Mehio
    London, UK

  2. Marlo said

    thank you for posting this very helpful tips, i solved a problem which i thought about a long time!

  3. 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.

  4. 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.

  5. iron kettle said

    Thanks so much for this code, going to try it now.

  6. Sathish Robert said

    Thank u

  7. Amit said

    Thanks a lot!! Your code sample was a godsend :-)

  8. 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.

  9. 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

  10. Banoth said

    Thanks a lot, excellent code, works perfect with the format in the country i live (netherlands)

  11. Elliot said

    I love it. Simple and effective. Thank you so much!

  12. This is code wonderful, high cohesion and very simple, thank you!

  13. 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:

      FormatRenderer center = NumberRenderer.getCurrencyRenderer();
      center.setHorizontalAlignment( SwingConstants.CENTER );
      

      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

  14. 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

    SimpleDateFormat format = new SimpleDateFormat(“yy/MM/dd”);
     //TableCellRenderer renderer = new FormatRenderer(format);
     TableColumnModel m = jTable.getColumnModel();
     m.getColumn(5).setCellRenderer(new FormatRenderer(format).getDateTimeRenderer());
    
    • 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:

      SimpleDateFormat format = new SimpleDateFormat(“yy/MM/dd”);
       m.getColumn(5).setCellRenderer(new FormatRenderer(format));
      

      The TableModel must also contain a object, not a String object.

  15. 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);

  16. 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.

  17. 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.

  18. 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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

Join 96 other followers

%d bloggers like this: