Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Formatting a JTable cell

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Marshal
Posts: 28288
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 28288
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
reply
    Bookmark Topic Watch Topic
  • New Topic