aspose file tools
The moose likes Swing / AWT / SWT and the fly likes Image Renderer for JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Reply Bookmark "Image Renderer for JTable" Watch "Image Renderer for JTable" New topic
Author

Image Renderer for JTable

Lavric Daniel
Greenhorn

Joined: Jun 15, 2011
Posts: 4
Hi! I'm new here but I want to post my solution for the ImageRenderer class:



Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4165
    
    3

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?


luck, db
There are no new questions, but there may be new answers.
Lavric Daniel
Greenhorn

Joined: Jun 15, 2011
Posts: 4
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

Joined: May 03, 2008
Posts: 4165
    
    3

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

Joined: Jun 15, 2011
Posts: 4
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

Joined: Oct 27, 2005
Posts: 19216

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.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Lavric Daniel
Greenhorn

Joined: Jun 15, 2011
Posts: 4
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!
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Image Renderer for JTable
 
Similar Threads
JTable
JScrollPane within JTable cell issue
Problem in JTable Renderer and Editor
making a JTable column not visible
How to show an image (jpg or gif) in a JTable cell ?