Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Icon to JFrame

Tags:

I tried this way, but it didnt changed?

ImageIcon icon = new ImageIcon("C:\\Documents and Settings\\Desktop\\favicon(1).ico"); frame.setIconImage(icon.getImage()); 
like image 826
vijay Avatar asked Mar 27 '13 11:03

vijay


People also ask

How do I add icons to Swing?

To add icon to a button, use the Icon class, which will allow you to add an image to the button. Icon icon = new ImageIcon("E:\editicon. PNG"); JButton button7 = new JButton(icon);

How do I change the JFrame icon in Netbeans?

The method setIconImage() of the JFrame class is used to change the icon of JFrame or JWindow. It changes the icon that is displayed on the left side of the window. The Toolkit class is used to get an instance of the Image class in AWT and Swing.


1 Answers

Better use a .png file; .ico is Windows specific. And better to not use a file, but a class resource (can be packed in the jar of the application).

URL iconURL = getClass().getResource("/some/package/favicon.png"); // iconURL is null when not found ImageIcon icon = new ImageIcon(iconURL); frame.setIconImage(icon.getImage()); 

Though you might even think of using setIconImages for the icon in several sizes.

like image 57
Joop Eggen Avatar answered Oct 21 '22 05:10

Joop Eggen