Java Tips Weblog

  • Blog Stats

    • 818,132 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, 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

Related Reading

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

About these ads

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

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 )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

Join 66 other followers

%d bloggers like this: