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

Image Renderer for JTable

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi! I'm new here but I want to post my solution for the ImageRenderer class:



 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Lavric, and welcome to the Ranch! It's nice that you like to share, but in future, please don't post something new in a topic thread that's only slightly related. The thread you posted this in was titled "Resizing ImageIcon" which is not the aim of your code, so I've split it out into its own thread.

Also, please go through Code Conventions for the Java TM Programming Language where you will learn about formatting your code with consistent indents. Also, you apparently have a lot to learn about using meaningful variable and method names, and about access specifiers and encapsulation.

Finally, your code completely ignores the Object value parameter to getTableCellRendererComponent(...), setting the same image to all cells of the table, which usually isn't the purpose of a renderer. Could you tell us what you had in mind when you wrote this class?
 
Lavric Daniel
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi again. You said

Also, you apparently have a lot to learn about using meaningful variable and method names, and about access specifiers and encapsulation.


And you are right. I am a beginner. I was searching for an ImageRenderer to set up only for a cell. But I admit that my class can only be used for a column (my mistake) as it follows:
ImageRenderer image = new ImageRenderer();
table.getColumnModel().getColumn(0).setCellRenderer(image);
And it was working, therefore I've got entusiastic, and I wanted to share. Sory, if did something wrong!
In fact I need help becouse as I was writing before, I fieger out that I need an ImageRenderer class to set up for a specific cell. I've seen a method of multirendering, or something like that, but it seems to be more complicated, and I need a lot of time to study and implement in my project.
Maybe I can get some help in this forum.
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To render an Icon in a JTable, it is sufficient to return Icon.class from the getColumnClass(...) of either the table or its model. If desired, you can populate the model with ImageIcons created form a scaled version.

Oversimplified example SSCCE using Icons from the UI Manager:
 
Lavric Daniel
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Darryl, but that doesen't help me that mutch, becouse I don't know haow to use a jpg or gif image instead of an icon:
{UIManager.getIcon(new ImageIcon(newImg))} it's not working.
Thanks any way, and maybe you can help me with the procedure.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ImageIcon implements Icon and can load its data directly from files, resources and even the Internet (the latter two using a URL argument). And if that's not an option you can use ImageIO's read methods to get a BufferedImage, that you can either put in the table directly (as JTable has automatic support for both Icon and Image), or wrap it into an ImageIcon.

You just should use those ImageIcons directly; get rid of the UIManager.getIcon call. That was only part of Darryl's example because he didn't want to load any external images.
 
Lavric Daniel
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

You just should use those ImageIcons directly; get rid of the UIManager.getIcon call.


Thanks! You've made my day! It's working like a charm!
 
reply
    Bookmark Topic Watch Topic
  • New Topic