This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Formatting a JTable cell  RSS feed

 
Marcelo Ruiz
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I would like to know if anyone could suggest me an easy way to achieve this:

I need a JTable to show a custom number in one its columns, and I need to ad a prefix to the number (the unit) that needs to be shown left aligned in the cell, and the number needs to be shown right aligned in the same cell. Examples:

"$ 100.00"
"$/hr 99.00"
"m3/s 444"

I know I need to use a custom cell renderer, but I'm having trouble deciding the most efficient way of implementing it. Any suggestions?

Thanks!
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are the ways you have to choose from so far?
 
Marcelo Ruiz
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul,

Unfortunately the examples weren't printed as I expected, so I will post them again as code (each will be in a different column):



At first I thought of using a DefaultTableCellRenderer to return a formatted String of the original value (for example the result of String.format("$/hr %1$d") but that does not solve the alignment problem. I don't know any way to specify this kind of compound-justified alignment with a format String...
I was also thinking of having a custom TableCellRenderer that will return a JPanel with two labels: the first (left oriented) for the unit, and the second (right oriented) for the value. But I feel that is somehow overkilling for cell renderer.
I can't think of another solution and I feel that I missing something that will make my life easier (just with that, of course! )
Do you have any suggestions?
Thanks,

Marcelo
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could put the prefix ("$") and the rest ("99.00") into separate columns, and use suitable renderers for each column.
 
Marcelo Ruiz
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul,

Thanks for your answer.
Unfortunately the users of the application are allowed to move the JTable's Columns around, and it will be awkward to have them moving the unit column and the the value one...
The worst part of this problem is that I feel this is something very common and it should have a simple solution... I am just unable to find it...
Do you think of another approach that might work?
Thanks!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!