How do I change the default application icon in Java?

JavaIcons

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

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

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

Solution 2 - Java

You can simply go Netbeans, in the design view, go to JFrame property, choose icon image property, Choose Set Form's iconImage property using: "Custom code" and then in the Form.SetIconImage() function put the following code:

Toolkit.getDefaultToolkit().getImage(name_of_your_JFrame.class.getResource("image.png"))

Do not forget to import:

import java.awt.Toolkit;

in the source code!

Solution 3 - Java

Or place the image in a location relative to a class and you don't need all that package/path info in the string itself.

com.xyz.SomeClassInThisPackage.class.getResource( "resources/camera.png" );

That way if you move the class to a different package, you dont have to find all the strings, you just move the class and its resources directory.

Solution 4 - Java

Try This write after

initcomponents();

setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("Your image address")));

Solution 5 - Java

    /** Creates new form Java Program1*/
    public Java Program1() 


    Image im = null;
    try {
    im = ImageIO.read(getClass().getResource("/image location"));
    } catch (IOException ex) {
    Logger.getLogger(chat.class.getName()).log(Level.SEVERE, null, ex);
    }
    setIconImage(im);

This is what I used in the GUI in netbeans and it worked perfectly

Solution 6 - Java

In a class that extends a javax.swing.JFrame use method setIconImage.

this.setIconImage(new ImageIcon(getClass().getResource("/resource/icon.png")).getImage());

Solution 7 - Java

You should define icons of various size, Windows and Linux distros like Ubuntu use different icons in Taskbar and Alt-Tab.

public static final URL ICON16 = HelperUi.class.getResource("/com/jsql/view/swing/resources/images/software/bug16.png");
public static final URL ICON32 = HelperUi.class.getResource("/com/jsql/view/swing/resources/images/software/bug32.png");
public static final URL ICON96 = HelperUi.class.getResource("/com/jsql/view/swing/resources/images/software/bug96.png");

List<Image> images = new ArrayList<>();
try {
    images.add(ImageIO.read(HelperUi.ICON96));
    images.add(ImageIO.read(HelperUi.ICON32));
    images.add(ImageIO.read(HelperUi.ICON16));
} catch (IOException e) {
    LOGGER.error(e, e);
}

// Define a small and large app icon
this.setIconImages(images);

Solution 8 - Java

You can try this one, it works just fine :

`	ImageIcon icon = new ImageIcon(".//Ressources//User_50.png");
	this.setIconImage(icon.getImage());`

Solution 9 - Java

inside frame constructor

try{	
       setIconImage(ImageIO.read(new File("./images/icon.png")));	
   }
catch (Exception ex){
       //do something
   }

Solution 10 - Java

Example:

URL imageURL = this.getClass().getClassLoader().getResource("Gui/icon/report-go-icon.png");
ImageIcon iChing = new ImageIcon("C:\\Users\\RrezartP\\Documents\\NetBeansProjects\\Inventari\\src\\Gui\\icon\\report-go-icon.png");      
btnReport.setIcon(iChing); 
System.out.println(imageURL);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBill the LizardView Question on Stackoverflow
Solution 1 - JavaJeeBeeView Answer on Stackoverflow
Solution 2 - JavaAyoub AneddameView Answer on Stackoverflow
Solution 3 - JavaJohn GardnerView Answer on Stackoverflow
Solution 4 - Javauser2895893View Answer on Stackoverflow
Solution 5 - Javauser1456935View Answer on Stackoverflow
Solution 6 - Javauser2601995View Answer on Stackoverflow
Solution 7 - Javaron190View Answer on Stackoverflow
Solution 8 - JavaSpicy strikeView Answer on Stackoverflow
Solution 9 - JavaAlex SView Answer on Stackoverflow
Solution 10 - JavaRrezart A. PrebrezaView Answer on Stackoverflow