Forums Register Login

getImageIcon() : how does it work?

+Pie Number of slices to send: Send
Hi all.
I'm trying to get an imageicon into BorderLayout and I'm getting this error message from the java console:
...
java.security.AccessControlException: access denied (java.io.FilePermission bottlenose.jpg read)
at java.security.AccessControlContext.checkPermission(Unknown Source)
...
This seems strange because following code works well:

but there seems to be no javax.swing equivalent for getImageIcon.
Of course I'm assuming I need to use either getDocumentBase() or getCodeBase() to get pass the "access denied" problem. Following code should work, but it doesn't

TIA :-)
I think the question should be "how does one use getDocumentBase() win getImageIcon()" ?
Without getDocumentBase(), it seems I cannot get the image.

Okay, this code works and fetch the "icon" back.
But resize() didn't iconify my picture ie. it didn't shrink it so that it becomes an icon. I was hoping to put the iconified barn owl next to the label text...

By the way, it doesn't seem to matter whether I use SWING's super.paintComponent(g) or not ...
[ November 10, 2003: Message edited by: achana chan ]
+Pie Number of slices to send: Send
The exception you are getting is because you are in a Java applet and Java applets aren't allowed to access other files. The method getImage() in the Applet/JApplet class is a way around this (I'm not entirely sure how it works, but it does).
Creating an ImageIcon using "new ImageIcon()" is not possible because it relies on normal I/O. So you get an access permission exception. Instead, use getImage() from the Applet class to create you're image. Then create a new ImageIcon using that Image object. This should get rid of that exception.
If you are doing anything in Swing you should use paintComponent() and NOT paint(). This stuffs up the way Swing does back buffering.
When you are setting the size of a JPanel, you should use setPreferredSize() instead of resize(). You will probably have more luck this way.
Hope that helps.
+Pie Number of slices to send: Send
Modifying the code has no visible effect. I can still get the image back. The problem seems not to be with getDocumentBase() or getCodeBase().
I just cannot iconify the image, when I use setPreferredSize(imageWidth,imageHeight), its more like a little viewport into a bigger picture.
+Pie Number of slices to send: Send
It's so late to answer, but to someone this could be useful ...

Daniel Searson wrote:The exception you are getting is because you are in a Java applet and Java applets aren't allowed to access other files. The method getImage() in the Applet/JApplet class is a way around this (I'm not entirely sure how it works, but it does). Creating an ImageIcon using "new ImageIcon()" is not possible because it relies on normal I/O. So you get an access permission exception.



That's true.

So, with ImageIcon you could do two things to avoid the AccessControlException because java.io.FilePermission:

1. To grant explicit permission to access local system resources ... resources that you will use in the applet.


You could see the next link about "Security and Permissions": http://java.sun.com/developer/onlineTraining/Programming/JDCBook/appA.html


2. To use ClassLoader

For example:


Everything is in the same path:

src/
- ImageIcon.java
- image.jpg
- index.html -> To run the applet

You could see the next link: http://www.leepoint.net/notes-java/GUI-lowlevel/graphics/45imageicon.html


By the way, ImageIcon doesn't allow to resize the image. You could see this post: https://coderanch.com/t/331731/GUI/java/Resize-ImageIcon. It works perfect.

Barry's not gonna like this. Barry's not gonna like this one bit. What is Barry's deal with tiny ads?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 5260 times.
Similar Threads
Label in a field or scrollpanel
small doubt in Swing appln using JPanel
Drawing
Display image in a BorderLayout
Graphics
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 03:13:54.