File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
A friendly place for programming greenhorns!
Big Moose Saloon
Register / Login
Swing / AWT / SWT
Dynamically setting an ImageIcon's image file name
Joined: Feb 07, 2013
Feb 07, 2013 12:04:58
I am working on a project and have come across a stumbling block when it comes to getting an image in a
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.
String imgProductStr = ""; ImageIcon imgProduct = new ImageIcon(imgProductStr); JLabel lblProductImg = new JLabel(imgProduct);
Now when the user clicks a button, actionPerformed checks the source and then asks a method called getImage (from Bclass) to return the
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
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
as "a.gif" with quotation marks but this has not worked either.
ImageIcon imgProduct = new ImageIcon("\""+imgProductStr+"\"");
Any help / pointers would be appreciated.
Joined: Feb 07, 2013
Feb 07, 2013 12:55:46
Any moderators able to delete this thread?
I have finally found out the answer after
a dozen of combinations. I had to creating a new instance of
when I receive the image file name.
Joined: Oct 27, 2005
Feb 07, 2013 15:27:02
We don't delete threads. Your solution may help someone who encounters the same problem and stumbles upon this thread.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions
How To Answer Questions
I agree. Here's the link:
subject: Dynamically setting an ImageIcon's image file name
ImageIcon returned from Event Handler
How to import Image file
Doubt regarding Constructors - - JLS
Problems dispaying icon
BufferedImage to Icon - have I done this in the correct way?
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter
| Powered by
Copyright © 1998-2014