Solved

How to read images from applet JAR file

Posted on 2001-09-06
6
251 Views
Last Modified: 2013-11-23
I have an applet whose required files (class, gif etc) are stored in a JAR file. I want to read some images from the JAR file to be used as button icons in the applet. I wrote a method to read from the JAR file and to return the image in a ImageIcon object:

public static ImageIcon loadIcon(String name) {
   try {
      ImageIcon imageIcon = new ImageIcon(ClassLoader.getSystemResource(name));
      return imageIcon;
   } catch (Exception exception) {}
      return null;
}

However, it doesn't work. I have also tried adding "/" in front of "name" but still won't work. Any help is appreciated.

BTW, if I run my program as a stand-alone application, the images are read successfully from the JAR file.
0
Comment
Question by:yongsing
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 16

Expert Comment

by:heyhey_
ID: 6460500
did you try

ImageIcon imageIcon = new ImageIcon(ClassLoader.getResource(name));
0
 
LVL 92

Expert Comment

by:objects
ID: 6462490
Use:

ImageIcon imageIcon =
 new ImageIcon(getClass().getResource(name));

0
 
LVL 9

Author Comment

by:yongsing
ID: 6462658
heyhey_, ClassLoader.getResource() is a non-static method. You can't call it from a static context.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Accepted Solution

by:
objects earned 50 total points
ID: 6462771
getClass().getResource(name) will work in an applet.
0
 
LVL 9

Author Comment

by:yongsing
ID: 6462856
Yes, I've tried it and it works. Thanks!
0
 
LVL 92

Expert Comment

by:objects
ID: 6462871
no worries.
Thanks for the points :)
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question