Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: ImageIcon not working. HELP!

  1. #1
    Prime624 is offline Member
    Join Date
    Feb 2013
    Location
    San Diego
    Posts
    96
    Rep Power
    0

    Default ImageIcon not working. HELP!

    When I try to make an ImageIcon, it fails to correctly set the image to the object. I have tried setting it in the declaration and with a command. I know this because when I print out the width and length of the Image, they both come up as -1. I have tried this on my computer (Windows 7) and my cousins computer (Ubuntu) and I get the same result.

    Any tips?

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

    Default Re: ImageIcon not working. HELP!

    Often the reason for this is that you're looking in the wrong location for the image or looking for it in the wrong way. For instance, if your image is in a jar file, then you can't look for it as a file, but rather as a class resource, and the relative path to the resource is relative to the location of the class files, not the user's directory. If the image is not in a jar, then you can look for it as a file, but have to know where the image is relative to the user's directory, something that you can find out easily by putting this in your code:

    Java Code:
    System.out.println(System.getProperty("user.dir"));
    The other option is to not try to use relative paths to the image but instead just use the full path to it.

  3. #3
    Prime624 is offline Member
    Join Date
    Feb 2013
    Location
    San Diego
    Posts
    96
    Rep Power
    0

    Default Re: ImageIcon not working. HELP!

    I fiddled around with the path and have somehow gotten it to work. Sorry for posting a self-solving problem, but it has been going on for a week and I couldn't figure it out. Thanks for your help

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: ImageIcon not working. HELP!

    Self-solving is just fine, as long as you've solved it, as long as you've learned, and as long as the solution is a good one.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: ImageIcon not working. HELP!

    Here's a good learning resource: Loading Images Using getResource

    db
    Fubarable likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Using ImageIcon
    By David1234204 in forum AWT / Swing
    Replies: 4
    Last Post: 03-21-2012, 08:14 PM
  2. How to add an ImageIcon on a JPanel???
    By Angelo in forum AWT / Swing
    Replies: 22
    Last Post: 12-19-2011, 02:27 PM
  3. Need help with ImageIcon and int
    By proceeded in forum New To Java
    Replies: 9
    Last Post: 01-23-2011, 10:10 PM
  4. ImageIcon HELP!
    By ben1989 in forum New To Java
    Replies: 1
    Last Post: 05-05-2010, 08:48 PM
  5. ImageIcon background
    By Yannick in forum New To Java
    Replies: 11
    Last Post: 02-12-2009, 04:13 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
  •