File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Dynamically setting an ImageIcon's image file name Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Dynamically setting an ImageIcon Watch "Dynamically setting an ImageIcon New topic

Dynamically setting an ImageIcon's image file name

Angelo Edades

Joined: Feb 07, 2013
Posts: 2
Hi All,

I am working on a project and have come across a stumbling block when it comes to getting an image in a JLabel to appear at all.

For now lets us assume the following:

a image file called a.gif is in the root directory of the project.

I have the following declared on my class called Aclass.

Now when the user clicks a button, actionPerformed checks the source and then asks a method called getImage (from Bclass) to return the string which is the file name as "a.gif".

Bclass.getImage queries the database and uses the appropriate column to return the file name for a given key. So far I have managed to get this bit right.

Back to Aclass, I manage to assign a.gif to imgProductStr and when I do a System.out.println(imgProductStr); within the logic it does print out a.gif which is correct.

A few lines down I use the following code to assign the file name to an instance of ImageIcon.

I am completely stumped.

All I can think of is that it is returning it as a.gif when really it should be going in to the instance of ImageIcon as "a.gif" with quotation marks but this has not worked either.

Any help / pointers would be appreciated.

Angelo Edades

Joined: Feb 07, 2013
Posts: 2
Any moderators able to delete this thread?

I have finally found out the answer after testing a dozen of combinations. I had to creating a new instance of ImageIcon when I receive the image file name.
Rob Spoor

Joined: Oct 27, 2005
Posts: 20273

We don't delete threads. Your solution may help someone who encounters the same problem and stumbles upon this thread.

How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: Dynamically setting an ImageIcon's image file name
It's not a secret anymore!