Solved

How to read images from applet JAR file

Posted on 2001-09-06
6
237 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
  • 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

776 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