• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Images in a JTable

 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I would like a column in my JTable to be one that displays one of two images. If the database returns true, it will display image 1 (a tick). If the database returns false, it will display image 2 (a cross). I know that by default the JTable returns a JLabel. Despite having the correct image icon in the [][]array that populates the JTable, when the table is run it simply returns the location of the image. This is to be expected. However, how can i change the JTable so it returns an ImageIcon and not its default JLabel? I am figuring the table renderer will come into play. I am already using a Table renderer like below. All that the renderer does at the minute is return different coloured rows. Anyone got any ideas how i can solve my problem? In case its significant, only column four of the JTable needs to return a ImageIcon, the other columns should remain as the default JLabel.




Any help would greatly be appreciated. Thanks
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not so much in how you define the cell renderer but in how you associate a renderer with a column. You can either explicitly associate a cell renderer with a TableColumn from the TableColumnModel, or change the default renderer for Boolean.class. Here is an example of the latter.
(Rather than defining new renderers and editors, I just change the icon used by them. Try clicking on the second column!)
[ January 17, 2006: Message edited by: Jeff Albrechtsen ]
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I should add that before you supply your own icons, have you thought about using the default?
 
Marcus Hathaway
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jeff for all your help and code. Has worked fine. All i needed to do was add:

public Class getColumnClass(int columnIndex)
{
return columnIndex == 3 ? ImageIcon.class : Object.class;
}

to my table model and it works a treat.

Thanks again!
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Marcus Hathaway:
return columnIndex == 3 ? ImageIcon.class : Object.class;


That works, but eventually you may want to rethink your TableModel. The TableModel *is* the data, and ImageIcon isn't really data -- I thought the data was a boolean., so an ImageIcon is more related to rendering the data. Where this could get you in trouble is if you want to programmatically access data in the table. Then you would have to reinterpret an ImageIcon as a true/false value...
 
Marcus Hathaway
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jeff,

Yes your right. However, in this particular instance, its not important for me to treat the image as anything other than that. But i totally take on board what you are saying, and in the example that you provided - doing it the 'simple' way like i have would not be possible.

I'm not sure if i may have to change my table model as you suggest anyway. As the code in my first post states, i am using a cell renderer to make the rows different colours. However, now that i have added the new code to my table model, the column which displays the image does simply that....it no longer has the row colour. So in columns 1, 2, 3, the row is fine with a nice blue or white background colour. However, column 4 simply displays the image and is no longer affected by the individual row colour. Anybody have any suggestions to how i can affect the row colour for column 4 again? Do i need to somehow register column 4 with the cell renderer again?

Again, any advice would be greatly appreciated.

Cheers!
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One common effect in a JTable is to have different background colors for rows. Apart from indicating selection, you many want the rows to alternate colors: white, light-gray, white, light-gray,... or have the color indicate some sort of priority (green, yellow, red) etc...

The easiest wat to do this is not to tinker with the renderers but to override JTable's prepareRender method. The reason this is preferred is because it cuts across all renderers: you often have several in a table: for text, numbers, currency, dates, booleans, images, etc... and you wouldn't want to repeat code in all those renders. There is an example of overriding prepareRender in this thread: https://coderanch.com/t/342132/GUI/java/JTable-different-rows-set-different
 
I will suppress my every urge. But not this shameless plug:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic