Results 1 to 8 of 8
  1. #1
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Unhappy How do I make ImageIcon strings shorter.

    This is probably a very simple answer, but in all my googling and searching I can't find it.

    My problem is this: Whenever I create an ImageIcon, I have to type a very long string starting at my hard drive.

    e.g:

    Java Code:
    ImageIcon myIcon = new ImageIcon("C:/Documents and Settings/ExampleUser/eclipse/workspace/Project/src/Mypackage/Icon.PNG")
    This is very long and tedious, and would not work on a different computer without the exact path. I am hoping someone could explain to me how to use shorter strings like this:

    Java Code:
    ImageIcon myIcon = new ImageIcon("/MyPackage/Icon.PNG")
    Please help me!!!


    Hopefully the code is correct, had to write it as I created this post.:eek:
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Place all the images in your project folder. Then you can access using the global path, simple path as you looking to use.

  3. #3
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    My images are within my project folder but in a different package. Does this matter? Or should they be in the same package, or directly under the project?
    Also if the project is MyProject using src as the source file, and the images are in package MyPictures, should this be the code?

    Java Code:
    ImageIcon myIcon = new ImageIcon ("MyProject/src/MyPictures/image.PNG")
    or is it this shorter version?
    Java Code:
    ImageIcon myIcon = new ImageIcon ("MyPictures/image.PNG")
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  4. #4
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    The "right" approach is to load your icons (and everything else that is relative to your project) through the ClassLoader.

    For example,
    Java Code:
    ImageIcon icon = new ImageIcon(Classloader.getSystemClassLoader().getResource("icons/myicon.png"));
    The classloader will find the resources in both a directory relative to your packages or even in an executable jar file. I usually make the ClassLoader a final static in my GUI classes to simplify the calls.

  5. #5
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Thanks Toadaly that seems to work. I still think that there is an easier way though. Hopefully someone mentions it. The ClassLoader seems very long.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    As regards "easier" and "very long", it doesn't get much easier than specifying the URL and having the ImageIcon create an icon from it! As toadaly mentioned if it is going to be used a lot the class loader could be a variable - or the getResource() could be a method if you want to get fancy and deal with missing images etc in a consistent way.

    To my mind the important thing is that it does use a URL rather than a filename. As you mentioned in the original post filenames become a bit of a problem when you run the application on a different computer from the one that compiled it.

    For completeness this short Java World article contrasts the two flavours of getResource (Class vs ClassLoader) and the author gives his reasons for sometimes preferring the former.
    Last edited by pbrockway2; 03-10-2009 at 10:14 AM.

  7. #7
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Thanks for the tip. I'll check it out
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  8. #8
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    hmm... I think it works but it also caused my window to close instantly. I think I'll stick with toadaly's (Classloader.getSystemClassLoader().getResource("i cons/myicon.png")) since I know that works
    Last edited by Singing Boyo; 03-11-2009 at 01:02 AM.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

Similar Threads

  1. ImageIcon background
    By Yannick in forum New To Java
    Replies: 11
    Last Post: 02-12-2009, 05:13 PM
  2. send imageIcon over RMI
    By nadia in forum Advanced Java
    Replies: 4
    Last Post: 01-06-2009, 04:41 PM
  3. ImageIcon and size
    By wokena in forum Java 2D
    Replies: 3
    Last Post: 12-21-2008, 09:47 AM
  4. Detect loading of ImageIcon from URL?
    By barkster in forum Java Applets
    Replies: 1
    Last Post: 01-29-2008, 08:04 PM
  5. Replies: 0
    Last Post: 08-06-2007, 07:54 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
  •