Results 1 to 10 of 10
  1. #1
    Manfizy's Avatar
    Manfizy is offline Senior Member
    Join Date
    Nov 2008
    Location
    Germany
    Posts
    193
    Rep Power
    7

    Post Uncaught error fetching image

    Hi people, i am trying to change the icon for a JFrame. my code is as follows
    Java Code:
    setIconImage(Toolkit.getDefaultToolkit().getImage(
           JFrame.class.getResource("Icons/icon.png")));
    anyone with an idea as to why i get this error?

    cheerz!
    We Learn Through Mistakes..,
    Manfizy:rolleyes:

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Do you need to place this code within a try/catch block?

  3. #3
    Manfizy's Avatar
    Manfizy is offline Senior Member
    Join Date
    Nov 2008
    Location
    Germany
    Posts
    193
    Rep Power
    7

    Post

    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 me
    We Learn Through Mistakes..,
    Manfizy:rolleyes:

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    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.

  5. #5
    Manfizy's Avatar
    Manfizy is offline Senior Member
    Join Date
    Nov 2008
    Location
    Germany
    Posts
    193
    Rep Power
    7

    Post

    I dont get an error but the normal java coffee cup icon is still displayed:confused:
    We Learn Through Mistakes..,
    Manfizy:rolleyes:

  6. #6
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    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);
        }
    }

  7. #7
    Manfizy's Avatar
    Manfizy is offline Senior Member
    Join Date
    Nov 2008
    Location
    Germany
    Posts
    193
    Rep Power
    7

    Post

    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:

  8. #8
    Manfizy's Avatar
    Manfizy is offline Senior Member
    Join Date
    Nov 2008
    Location
    Germany
    Posts
    193
    Rep Power
    7

    Thumbs up

    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"".


    Java Code:
    (Toolkit.getDefaultToolkit().getImage(
           MainJFrame.class.getResource("icon.jpg")));
    This is an example when I have file icon.jpg changing image in a window of class MainJFrame.

    Cheerz!!
    We Learn Through Mistakes..,
    Manfizy:rolleyes:

  9. #9
    zwarmapapa is offline Member
    Join Date
    Dec 2011
    Posts
    1
    Rep Power
    0

    Default Re: Uncaught error fetching image

    setIconImage(Toolkit.getDefaultToolkit().getImage(
    JFrame.class.getResource("Icons/icon.png")));
    You forgot an "/" before "Icons/icon.png"
    It should be "/Icons/icon.png"
    Last edited by zwarmapapa; 12-03-2011 at 04:17 PM. Reason: added quote tags

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default 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.
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Do uncaught errors appear in the PrintStream?
    By arnab321 in forum New To Java
    Replies: 1
    Last Post: 02-26-2009, 09:56 AM
  2. error encountered when writing image file
    By angelinehelena in forum Advanced Java
    Replies: 1
    Last Post: 02-10-2009, 11:08 AM
  3. error while loading the image from the server
    By testtest in forum CLDC and MIDP
    Replies: 1
    Last Post: 09-23-2008, 06:16 AM
  4. error reported when binarization of gray image
    By Mazharul in forum Java 2D
    Replies: 1
    Last Post: 09-18-2008, 10:45 AM
  5. Replies: 2
    Last Post: 07-13-2008, 06:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •