llm
llm

Reputation: 5669

Format a number as currency in a JTable?

Given a JTable where one of the columns contains a number, how do I display this number as a currency? I.e. 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 have to do this earlier? Thanks.

Upvotes: 2

Views: 7957

Answers (2)

Yishai
Yishai

Reputation: 91881

There are a couple of ways to approach this. The up-front easiest might be to simply populate that column with a custom object that implements the currency formatting in its toString() method.

The other is to use a custom cell renderer. Here is some sample code for setting that up. It would have the advantage of being "done directly on the JTable after it has been populated with data."

Upvotes: 2

camickr
camickr

Reputation: 324118

Table Format Renderers shows how numbers and dates can easily be formatted.

Upvotes: 1

Related Questions