开发者

Format a number as currency in a JTable?

开发者 https://www.devze.com 2022-12-27 15:55 出处:网络
Given a JTable where one of the columns contains a number, how do开发者_Python百科 I display this number as a currency? I.e. 5 should display as $5.00 etc.

Given a JTable where one of the columns contains a number, how do开发者_Python百科 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.


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号