Mac does not support frame icons, as seen in this answer.
setImageIcon doesn't set JFrame icon on mac swing window
Question
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
Solution
OTHER TIPS
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);
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);
}