3

I've already tried loads of code from Stack. For some reason it's just not setting the ImageIcon for my JFrame, the comments are other attempts that have not worked;I avoided calling super so that I could reference the JFrame -- GUIPhotoAlbum extends JFrame; code:

public GUIPhotoAlbum ()
{
    super("PhotoAlbum");
    ImageIcon img = new ImageIcon("Photos/albumIcon.png");
    this.setIconImage(img.getImage());

    /*
    try{
        setIconImage(ImageIO.read(new File("Photos/albumIcon.png")));
    }catch(Exception e){
        System.out.print("Didn't work.");
    }
    */

    setSize(875, 625);
    this.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    this.setLayout(new BorderLayout(5, 5));

    initComponents();
    initMenuBar();

    initTopPanel();
    add(topPanel, BorderLayout.CENTER);

    initBottomPanel();
    add(bottomPanel, BorderLayout.SOUTH);

    addListeners();

    setLocationRelativeTo(null);
    setVisible(true);
}

EDIT I'm running the program like this, where I try to set the ImageIcon of JFrame in the GUIPhotoAlbum() constructor; here's the driver:

public class AlbumDriver
{   
    public static void main (String [ ] args)
    {
           SwingUtilities.invokeLater 
           (
                 new Runnable()
                 {
                        @Override
                        public void run()
                        {
                            GUIPhotoAlbum pa = new GUIPhotoAlbum();
                        }   
                 }
           ); 
    }

}

What am I doing wrong here? PS I've tried BufferedImage, ImageIcon, using File.. and I'm using a Mac

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Chizx
  • 351
  • 2
  • 5
  • 16
  • 1
    Where is `Photos/albumIcon.png` located is it within the application Jar or one the file system? – MadProgrammer Apr 30 '14 at 01:27
  • I guess the file system? It's just a subfolder of the project.. `Photos` that is – Chizx Apr 30 '14 at 01:27
  • Start by using `ImageIO.read(new File("Photos/albumIcon.png"))`, this will throw an `IOException` if the file can't be loaded for some reason. Make sure that the program is been executed in the same directory as the `Photos` directory – MadProgrammer Apr 30 '14 at 01:29
  • The exception is never thrown.. let me make an edit real quick to explain how I'm running this, also I've just been thinking the whole time that perhaps the mac-java window doesn't display it for some reason but I don't think this is the case – Chizx Apr 30 '14 at 01:31
  • Does `GUIPhotoAlbum` extend from `JFrame`? – MadProgrammer Apr 30 '14 at 01:37

3 Answers3

6

Mac does not support frame icons, as seen in this answer.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
4

Use this to change Dock Image in mac:

File imageFile = new File("Your image Path");
Image image =  ImageIO.read(imageFile);
Application.getApplication().setDockIconImage(image);

For windows use this:

YourFrameObject.setIconImage(image);
Katta Nagarjuna
  • 1,539
  • 2
  • 12
  • 12
2

The problem is, you class appears to be extending from JFrame but you're creating a new instance of a JFrame and setting it's icon instead...

JFrame newFrame = new JFrame("PhotoAlbum");

ImageIcon img = new ImageIcon("Photos/albumIcon.png");
newFrame.setIconImage(img.getImage());

Don't create the second instance of the JFrame, there's no need for newFrame in this instance...

For example...

public GUIPhotoAlbum ()
{
    super("PhotoAlbum");
    ImageIcon img = new ImageIcon("Photos/albumIcon.png");
    setIconImage(img.getImage());

    /*
       //when uncommented, exception is never thrown
    try{
        setIconImage(ImageIO.read(new File("Photos/albumIcon.png")));
    }catch(Exception e){
        System.out.print("Didn't work.");
    }
    */

    // Hint use pack instead, but only after
    // You've finished adding the components to the frame
    setSize(875, 625);
    setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    setLayout(new BorderLayout(5, 5));

    initComponents();
    initMenuBar();

    initTopPanel();
    add(topPanel, BorderLayout.CENTER);

    initBottomPanel();
    add(bottomPanel, BorderLayout.SOUTH);

    addListeners();

    setLocationRelativeTo(null);
    setVisible(true);
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Ah.. I see, I also tried setting it to "pa" in the driver, that didn't work either – Chizx Apr 30 '14 at 01:40
  • Sorry pa is the name of the new GUIPhotoAlbum in the driver class' main method. I was just setting this up to and it didn't work... I'll update top code.. Yeah this is what I was trying earlier before incorrectly trying to create the new JFrame. This isn't working either, and `pack()` messes up the program because I have an empty JPanel on the bottom that gets populated with Photos – Chizx Apr 30 '14 at 01:44
  • I think the problem is that it's mac, I haven't tried running this on Windows but I think it would work on windows. – Chizx Apr 30 '14 at 01:53
  • I'm pretty sure that you can set the icon of a mac window...or least it worked last time I bothered to try – MadProgrammer Apr 30 '14 at 01:54