Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: I cant load the icon!!!!

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default I cant load the icon!!!!

    import java.awt.Image;
    import java.awt.Toolkit;
     
    import javax.swing.JFrame;
     
    public class Main {
      public static void main(String[] args) {
     
        JFrame frame = new JFrame();
     
        Image icon = Toolkit.getDefaultToolkit().getImage("mercuryLogo.jpg");
        frame.setIconImage(icon);
       frame.setVisible(true);
     
      }
    }

    im getting frustrated here..... please help.. i cant load the icon for the jframe


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: I cant load the icon!!!!

    What errors are you getting if any?

    Edit: make sure the image is in the right path or on the classpath. If you try giving a full path to the image it will most likely work as in (c:/pictures/mercuryLogo.jpg)

    // Json
    Last edited by Json; January 20th, 2010 at 06:38 AM.

  3. The Following User Says Thank You to Json For This Useful Post:

    chronoz13 (January 20th, 2010)

  4. #3
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Smile Re: I cant load the icon!!!!

    never had any idea that i have to set the path of the file exactly ... all the references that im reading in google is just telling me that i just have to type the file name including the file type.... "imge.jpg" ...

    tnx sir Json!..

  5. #4
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: I cant load the icon!!!!

    the icon is alreay loaded in the frame.. a follow up question.. if i load the image in a frame.. will the JVM set the image size into a default? i mean theres no way i can adjust its dimension just a little..?

  6. #5
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: I cant load the icon!!!!

    I dont know how the resizing works on icons to be honest, however when it comes to putting a specific location down you might not want to do that when you ship your program so you need to make sure the picture is on the classpath in that case.

    // Json

  7. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: I cant load the icon!!!!

    An alternative method to load the image is to load it as a resource.

    	java.net.URL imgURL = getClass().getResource(path);	//where path is a string path relative to your class
    	String pt = imgURL.getPath();			
    	if (imgURL != null) {
    		return new ImageIcon(imgURL, description);
    	} else {
    		System.err.println("Couldn't find file: " + path);
    	}

    In many cases I prefer this method because you can ship the icons within a jar. The downside is that loading things this way is slower (for whatever reason) than using the Toolkit loader, so its usually only useful for smaller images. Along the same lines of Json's suggestion of image paths, if I were to do the toolkit approach, I'll usually place the image in a path local to the app, and load it be creating a temp local file from which I can get the full path and with a little string changes get the path to the image

  8. The Following User Says Thank You to copeg For This Useful Post:

    chronoz13 (January 21st, 2010)

  9. #7
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: I cant load the icon!!!!

    using this way of yours sir copeg.. what method should i use to call this object to load an image..

    the setIconImage(), doesnt accept a URL object..

  10. #8
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: I cant load the icon!!!!

    and which of the two is better to prevent some loading errors..
    which one do you prefer most?

  11. #9
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: I cant load the icon!!!!

    I dont know how the resizing works on icons to be honest, however when it comes to putting a specific location down you might not want to do that when you ship your program so you need to make sure the picture is on the classpath in that case.
    can you check this one sir Json.. this one adjusts the image... in a quite amount
    .getScaledInstance(300, 300, 0)

    the complete block for my image icon is this
    Image icon = new ImageIcon("c:/pics/mercuryLogo.jpg").getImage().getScaledInstance(300, 300, 0);

  12. #10
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: I cant load the icon!!!!

    Quote Originally Posted by chronoz13 View Post
    the setIconImage(), doesnt accept a URL object..
    Sorry, I mis-read your original post and thought you were just trying to create an ImageIcon. Anyway, from a URL you could create a 'dummy' ImageIcon and retrieve the image that way (getImage()) or use ImageIO.read() which can read a URL and returns a BufferedImage (extends Image).

    Quote Originally Posted by chronoz13 View Post
    and which of the two is better to prevent some loading errors..
    which one do you prefer most?
    Again, it depends upon what you wish to do. Getting the image via the class loader allows you to package your application and associated icons/images inside a ja. The concepts are somewhat the same though in that rather than specifying a global path to the image, which can move from user to user and platform to platform, the image should be placed in a location relative to the application so you can let the app find the image as opposed to relying on a constant path to the image
    Last edited by copeg; January 21st, 2010 at 11:34 AM.

  13. #11
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: I cant load the icon!!!!

    Again, it depends upon what you wish to do. Getting the image via the class loader allows you to package your application and associated icons/images inside a ja. The concepts are somewhat the same though in that rather than specifying a global path to the image, which can move from user to user and platform to platform, the image should be placed in a location relative to the application so you can let the app find the image as opposed to relying on a constant path to the image

    so you mean sir.. URL class is better for getting an image in my case..?... i will just follow your suggestions.. i dont want to take any risk in my project....tnx!

    anyway..

  14. #12
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: I cant load the icon!!!!

    Quote Originally Posted by chronoz13 View Post
    URL class is better for getting an image in my case..?
    No. I just meant to reiterate Json's point - access your image relative to your application and not relying on something like:
    'c:/pictures/mercuryLogo.jpg' actually being there. Using the class path or getting the path of your app allows you to do so.

  15. The Following User Says Thank You to copeg For This Useful Post:

    chronoz13 (January 22nd, 2010)

  16. #13
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: I cant load the icon!!!!

    How about something like this.

            final URL url = ClassLoader.getSystemClassLoader().getResource("mercuryLogo.jpg");
            final Image icon = Toolkit.getDefaultToolkit().getImage(url);

    Oh and make sure your "mercuryLogo.jpg" file is in the root of the jar file that you have packaged together

    // Json

  17. The Following User Says Thank You to Json For This Useful Post:

    chronoz13 (January 22nd, 2010)

Similar Threads

  1. quite small (make the changelog non-static, i.e. load its content from a file)
    By Abdallah in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: November 30th, 2009, 09:00 PM
  2. Arraylist Save and Load
    By frankycool in forum Collections and Generics
    Replies: 1
    Last Post: November 14th, 2009, 06:48 AM
  3. how to load a class and type cast to that class?
    By chinni in forum Object Oriented Programming
    Replies: 2
    Last Post: November 9th, 2009, 10:18 AM
  4. Icon change and lib folder problem
    By LeonLanford in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 21st, 2009, 03:00 AM