Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I change the default application icon in Java?

Tags:

java

icons

I'm using NetBeans, trying to change the familiar Java coffee cup icon to a png file that I have saved in a resources directory in the jar file. I've found many different web pages that claim they have a solution, but so far none of them work.

Here's what I have at the moment (leaving out the try-catch block):

URL url = new URL("com/xyz/resources/camera.png"); Toolkit kit = Toolkit.getDefaultToolkit(); Image img = kit.createImage(url); getFrame().setIconImage(img); 

The class that contains this code is in the com.xyz package, if that makes any difference. That class also extends JFrame. This code is throwing a MalformedUrlException on the first line.

Anyone have a solution that works?

like image 275
Bill the Lizard Avatar asked Oct 16 '08 18:10

Bill the Lizard


People also ask

How do I change the icon of a jar file?

You can create a shortcut for the . jar and then you can change it: Right button >> Properties >> Change Icon.

What is an icon in Java?

Icon is small fixed size picture, typically used to decorate components. ImageIcon is an implementation of the Icon interface that paints icons from images. Images can be created from a URL, filename, or byte array.

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

java.net.URL url = ClassLoader.getSystemResource("com/xyz/resources/camera.png"); 

May or may not require a '/' at the front of the path.

like image 114
JeeBee Avatar answered Oct 09 '22 11:10

JeeBee