Results 1 to 10 of 10
Thread: Uncaught error fetching image
- 07-21-2009, 12:40 PM #1
Uncaught error fetching image
Hi people, i am trying to change the icon for a JFrame. my code is as follows
anyone with an idea as to why i get this error?Java Code:setIconImage(Toolkit.getDefaultToolkit().getImage( JFrame.class.getResource("Icons/icon.png")));
cheerz!We Learn Through Mistakes..,
Manfizy:rolleyes:
-
Do you need to place this code within a try/catch block?
- 07-21-2009, 01:02 PM #3
Since am using NetBeans IDE, i am placing the code in JFrame properties / imageicon/ custom code
I dont think there should be a try catch block there BUT if its possible correct meWe Learn Through Mistakes..,
Manfizy:rolleyes:
- 07-21-2009, 06:32 PM #4
JFrame.class.getResource
The getResource method is a Class method. It delegates to the class loader. The class loader is the one which loaded the enclosing class into the jvm. Using JFrame.class will not get the class loader. Use the name of the enclosing class. You can use the getClass method for this if not in static context.
- 07-22-2009, 08:54 AM #5
I dont get an error but the normal java coffee cup icon is still displayed:confused:
We Learn Through Mistakes..,
Manfizy:rolleyes:
- 07-22-2009, 03:45 PM #6
There some possibilities to consider:
Using the antique Toolkit loading methods requires a MediaTracker to actually load the image data.
To get the loading status you'll have to ask the MediaTracker using some of its methods.
Besides the possibility that the image data may not have gotten loaded there could also be trouble with the image path/location, an image file that cannot be read/interpreted or that has corrupted data.
The MediaTracker api has example code to show how to use it to load images.
Just for fun you could try using the newest image loading techniqe. It throws an exception if the image data is not successfully loaded.
Java Code:import java.awt.image.BufferedImage; import java.io.IOException; import java.net.URL; import javax.swing.JFrame; import javax.imageio.ImageIO; public class Test { public static void main(String[] args) { URL url = Test.class.getResource(//"Icons/icon.png"); "images/middle.gif"); // where are we looking for this image System.out.println("url = " + url); BufferedImage image = null; try { image = ImageIO.read(url); } catch(IOException e) { System.out.println("read error: " + e.getMessage()); } JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setIconImage(image); f.setVisible(true); } }
- 07-22-2009, 05:03 PM #7
Thanks for the reply. this works pretty well on it own
where am i supposed to insert this in Netbeans since the code is generated?We Learn Through Mistakes..,
Manfizy:rolleyes:
- 07-23-2009, 11:47 AM #8
I finally got it working. For netbean users setIconImage you can set this directly in NetBeans properties. You have to open window of iconImage property and then choose to "set property using "custom code"".
This is an example when I have file icon.jpg changing image in a window of class MainJFrame.Java Code:(Toolkit.getDefaultToolkit().getImage( MainJFrame.class.getResource("icon.jpg")));
Cheerz!!We Learn Through Mistakes..,
Manfizy:rolleyes:
- 12-03-2011, 03:16 PM #9
Member
- Join Date
- Dec 2011
- Posts
- 1
- Rep Power
- 0
Re: Uncaught error fetching image
You forgot an "/" before "Icons/icon.png"setIconImage(Toolkit.getDefaultToolkit().getImage(
JFrame.class.getResource("Icons/icon.png")));
It should be "/Icons/icon.png"Last edited by zwarmapapa; 12-03-2011 at 03:17 PM. Reason: added quote tags
- 12-03-2011, 04:16 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,372
- Blog Entries
- 7
- Rep Power
- 17
Re: Uncaught error fetching image
Pleased don't resurrect old zombie threads and b.t.w. it depends where the resource is located w.r.t. the class.
kind regards,
Jos
ps. I'm closing this zombie thread before more guesses make it in here.When people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Do uncaught errors appear in the PrintStream?
By arnab321 in forum New To JavaReplies: 1Last Post: 02-26-2009, 08:56 AM -
error encountered when writing image file
By angelinehelena in forum Advanced JavaReplies: 1Last Post: 02-10-2009, 10:08 AM -
error while loading the image from the server
By testtest in forum CLDC and MIDPReplies: 1Last Post: 09-23-2008, 05:16 AM -
error reported when binarization of gray image
By Mazharul in forum Java 2DReplies: 1Last Post: 09-18-2008, 09:45 AM -
Uncaught exception java/lang/NoClassDefFoundError: org/bouncycastle/crypto/BlockCiphe
By Dhiraj.karalkar in forum CLDC and MIDPReplies: 2Last Post: 07-13-2008, 05:46 PM


LinkBack URL
About LinkBacks

Bookmarks