CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: window icon

  1. #1
    Join Date
    Sep 2011
    Posts
    15

    window icon

    Hi everyone, I need to set an image to the window of my application
    I've heard it is this way

    frame.setIconImage(new ImageIcon(imgURL).getImage());

    but not working, I know setIconImage needs an Image as a parameter, but I don't know how to create it because nowhere it asks for the location of my image

    another question:is the window icon (in the upper-left) the same as the task bar icon, or do I have to set two differents sized icons?

    thanks!

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: window icon

    but not working, I know setIconImage needs an Image as a parameter, but I don't know how to create it because nowhere it asks for the location of my image
    'imgURL' is a URL that holds the location of your image.

    another question:is the window icon (in the upper-left) the same as the task bar icon, or do I have to set two differents sized icons?
    The java docs says "Note : Native windowing systems may use different images of differing dimensions to represent a window, depending on the context (e.g. window decoration, window list, taskbar, etc.). They could also use just a single image for all contexts or no image at all."

    There is a method setIconImages() to allow you to set images of different sizes if required. On Windows a single image will be scaled to suit the various uses.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Sep 2011
    Posts
    15

    Re: window icon

    I changed imgURL with "/resources/img.png" but still not working

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: window icon

    Are you sure that is the correct relative path for the file?
    Trying creating a File object with that path and see if the file exists.
    ie:
    Code:
    System.out.println("file exists = "+new File("/resources/img.png").exists());
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  5. #5
    Join Date
    Sep 2011
    Posts
    15

    Re: window icon

    File file = new File("/resources/minilogo.png");
    System.out.println(file.exists());

    it throws me false, and the file.getAbsolutePath() is C:\resources\logo.png , but it is in my project folder, also I know \ path separators for windows are not allowed to pass in a String, instead I know to scape them. What can I do?

    thanks

  6. #6
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: window icon

    file.getAbsolutePath() is C:\resources\logo.png
    Is that where the file is: C:\resources\
    That is where the JVM is looking for it.
    Norm

  7. #7
    Join Date
    Sep 2011
    Posts
    15

    Re: window icon

    how can I change the absolutePath to point my neatBeans project, so anytime I want to get a resource, I just need to put /resources/img?
    thanks Norm

  8. #8
    Join Date
    May 2009
    Posts
    2,413

    Re: window icon

    You could try this. Replace MyIcon.gif with the name of your image file. Place it where you have the rest of your program.

    import java.awt.Image;
    import java.awt.Toolkit;

    Image image = Toolkit.getDefaultToolkit().getImage("MyIcon.gif");
    frame.setIconImage(image);

  9. #9
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: window icon

    Try this to see if you know what the absolute path is:
    File file = new File("<Put absolute path here>minilogo.png");
    then test if it exists
    Norm

  10. #10
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: window icon

    Try the following code which gets the file using the path relative to the classes location:

    Code:
    URL logoURL = MyClass.class.getResource("resources/img.png");
    ImageIcon appLogo  = new ImageIcon(logoURL);
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured