• Post Reply Bookmark Topic Watch Topic
  • New Topic

Image Renderer for JTable  RSS feed

 
Lavric Daniel
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! I'm new here but I want to post my solution for the ImageRenderer class:



 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • 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
  • 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
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Rob Spoor
Sheriff
Posts: 20898
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!