Format number as currency in JTable?

Given a JTable where one of the columns contains a number, how do I display that number as currency? That is, 5 should display as $ 5.00, etc.

Can this be done directly on the JTable after it has been populated with data, or do I need to do this earlier? Thanks.

+2


a source to share


2 answers


There are several ways to approach this. The easiest way is to just populate this column with a custom object that implements currency formatting in its method toString()

.



Another is to use a custom cell renderer. Here's a sample code to set it up. The advantage of this would be that it was "made directly onto the JTable after it was filled with data".

+2


a source


The Renderers table format shows how numbers and dates can be easily formatted.



+1


a source







All Articles