Solved

How to read images from applet JAR file

Posted on 2001-09-06
6
232 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now