Hey, I'm hoping someone can give me a hand here...I'm currently writing a JApplet which uses several buttons on several different JPanels.
Two of these buttons use an image and label (e.g. I did contestant.setIcon(cc); contestant.setText("C.C."); where contestant is the button, and all formatting occurs just after and the Image was instantiated using ImageIcon cc = new ImageIcon("cc.jpg") earlier.)
Unrelated, but if you click the buttons, the icons and labels change.

Now here's the thing: when I run as an applet within my IDE, everything works perfectly. When I write up a quick HTML file and try to run the applet in it, I get an error as soon as it tries to load the images:

java.security.AccessControlException: access denied (java.io.FilePermission cc.jpg read)
at java.security.AccessControlContext.checkPermission(Unknown Source)
at java.security.AccessController.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkRead(Unknown Source)
at sun.awt.SunToolkit.getImageFromHash(Unknown Source)
at sun.awt.SunToolkit.getImage(Unknown Source)
at javax.swing.ImageIcon.<init>(Unknown Source)
at javax.swing.ImageIcon.<init>(Unknown Source)
at Contestants.addStuff(Contestants.java:48)
at Contestants.<init>(Contestants.java:32)
at TopPanel.<init>(TopPanel.java:26)
at FTVLeague.init(FTVLeague.java:28)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Exception: java.security.AccessControlException: access denied (java.io.FilePermission cc.jpg read)

I'm not sure how to work around this error; I haven't the slightest clue why it would pull up an access denied error (especially when it works just fine out of my IDE). After a lot of web searching, the only solutions I could find all used buffered images - which you can't apply to JButtons (can only use ImageIcon - which can be converted from an Image).

If anyone could give me a hand with a work around, I'd greatly appreciate it. If there's any part of my code you think you need to see, I'll supply snippets.

Okay, I found my solution, I'll go ahead and state it here so anyone else who has a similar problem will know.

Basically, the reason it gives the error is because the original line of code tells the java program to look on the local machine - a big no-no. The work around is to make it look at a 'url' (albeit, not truly a full URL).
Simply replace the old block with this: this.getClass().getResource("image.jpg") Doing so basically tells it to look in the directory the .class file is in - which when it's online is a url. You can also have it look in sub directories by replacing the "image.jpg" with the directory - such as "images/image.jpg".

Relatively quick and painless fix - especially since I intentionally wrote my code so if I had to make any modifications I only had to do it once and it'd propagate throughout the rest of the program ^_^

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.