File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes getImageIcon() : how does it work? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "getImageIcon() : how does it work?" Watch "getImageIcon() : how does it work?" New topic
Author

getImageIcon() : how does it work?

achana chan
Ranch Hand

Joined: Jul 29, 2002
Posts: 277
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 ]

humanum errare est.
Daniel Searson
Ranch Hand

Joined: Dec 03, 2000
Posts: 83
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.


- Daniel
achana chan
Ranch Hand

Joined: Jul 29, 2002
Posts: 277
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.
Santiago Escobar Robledo
Greenhorn

Joined: Apr 17, 2011
Posts: 1
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: http://www.coderanch.com/t/331731/GUI/java/Resize-ImageIcon. It works perfect.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: getImageIcon() : how does it work?