aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Loading an Image Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Loading an Image" Watch "Loading an Image" New topic
Author

Loading an Image

Radha MahaLakshmi
Ranch Hand

Joined: Mar 28, 2003
Posts: 45
Hi All
I am trying to load an image to display in JLabel.
I am doing it in the following way.
ImageIcon icon = ClassLoader.getSystemResource("images/logo.gif");
JLabel title = new JLabel(" Commingled Funds Trade Entry ",icon,JLABEL.Left)
my gif file is in c:/images/logo.gif;
i am running this from c:/ as
java com.chase.impb.tokyo.matrix2.cfunds.CFTradeEntry
its worknig fine.
when i make a jar file for this class file and copy into d:/test and run the jar file from d:/test its not able to locate the image file.
Can somebody help me.
Do i need to pur the image also inside jar???
Regards
Radha
Silvio Fragata da Silva
Greenhorn

Joined: Sep 09, 2003
Posts: 3
Hello Radha
First: The getSystemResource of the class ClassLoader don�t returns a ImageIcon, it returns a URL !!
When you put all classes into a jar file, you must to use all pictures into jar too !!!

Originally posted by Radha MahaLakshmi:
Hi All
I am trying to load an image to display in JLabel.
I am doing it in the following way.
ImageIcon icon = ClassLoader.getSystemResource("images/logo.gif");
JLabel title = new JLabel(" Commingled Funds Trade Entry ",icon,JLABEL.Left)
my gif file is in c:/images/logo.gif;
i am running this from c:/ as
java com.chase.impb.tokyo.matrix2.cfunds.CFTradeEntry
its worknig fine.
when i make a jar file for this class file and copy into d:/test and run the jar file from d:/test its not able to locate the image file.
Can somebody help me.
Do i need to pur the image also inside jar???
Regards
Radha


Silvio Fragata da Silva
Radha MahaLakshmi
Ranch Hand

Joined: Mar 28, 2003
Posts: 45
Hi
Thanks for the reply.
Yeah i am using thst URL to construct object of ImageIcon like below.
new ImageIcon(imgURL, description).
Could you please let me know how to put imgae in jar file
my imgae is in a directory c:/images/logo_jpmf.gif.
while creating jar file if i say
jar -cf cfunds.jar /com/chase/impb/tokyo/matrix2/cfunds/*.class ,/images/logo_jpmf.gif
its giving error.
Can you please tell me how to create a jar file by includingh the image.
After that do i need to copy the image where ever i put this jar or would it take from jar file only???
Regards
Radha
[ September 09, 2003: Message edited by: Radha MahaLakshmi ]
[ September 09, 2003: Message edited by: Radha MahaLakshmi ]
Radha MahaLakshmi
Ranch Hand

Joined: Mar 28, 2003
Posts: 45
Hi
I am able to include the image in jar file but still its not able to locate the file.
here i am pasting the code
ImageIcon createImageIcon(String path, String description)
{
System.out.println("-------path:"+path);
try
{
URL imgURL = ClassLoader.getSystemResource(path);
System.out.println("-------imgURL:"+imgURL);
if (imgURL != null) {
return new ImageIcon(imgURL, description);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}catch(Exception e)
{
e.printStackTrace();
}
return null;
}
this function i am using as below
ImageIcon icon = createImageIcon("images/logo_jpmf.gif", "logo");
JLabel title = new JLabel(" Commingled Funds Trade Entry ", icon, JLabel.LEFT);

Regards
Radha
Silvio Fragata da Silva
Greenhorn

Joined: Sep 09, 2003
Posts: 3
The path into your jar is equals to the path into code
Ex.: "images/logo_jpmf.gif"
into jar
archive.jar
images
|
---logo_jpmf.gif
Radha MahaLakshmi
Ranch Hand

Joined: Mar 28, 2003
Posts: 45
Hi
i could able to put my image files inside jar and whn i run its not able to find the files.
Still its not able to find the images.
Is thare any thing wrong in my code???
Here is the functions i am using
ImageIcon createImageIcon(String path, String description) {
System.out.println("-------path:"+path);

URL imgURL = ClassLoader.getSystemResource(path);

System.out.println("-------imgURL:"+imgURL);
if (imgURL != null) {
return new ImageIcon(imgURL, description);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
above function i am usng as below
ImageIcon icon = createImageIcon("images/logo_jpmf.gif", "logo");
JLabel title = new JLabel(" Commingled Funds Trade Entry ", icon, JLabel.LEFT);
Regards
Radha
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Loading an Image