Question

I am creating an windows desktop swt application.

I need to change the frame icon, for that I used

frame.setIconImage((new ImageIcon("C:\\Documents and Settings\\arjuns\\Desktop\\logo1 copy.png")).getImage());

The icon is displaying when I manually run the code from eclipse, but when I create an installer using Install4j the icon is not appearing.

Can anyone please help me.

enter image description here

Was it helpful?

Solution

URL url = ClassLoader.getSystemResource("ressources/logo.png");
Toolkit kit = Toolkit.getDefaultToolkit();
Image img = kit.createImage(url);
setIconImage(img);

This is similar to the previous answer, but I need to add a bit of information.

You can still use a direct path to your image (C:/User/logo.png) BUT imagine you give your program to someone else, he wont have the image in that specific path.

So I recemmend you insert it in your project like so:

enter image description here

(I usualy do a sperate package for any ressources).

so it will become ressources/logo.png and it will work for anybody opening your project.

OTHER TIPS

 import java.awt.*;
 import javax.swing.*;
  class set extends  JFrame
 {
set()
{
    setSize(100,100);
    setVisible(true);
    setIconImage(new ImageIcon("navbit-home.png").getImage());
}

public static void main (String[] args) {
new set();
}
}

enter image description here

please set appropriate path.like C:/Documents and Settings/arjuns/Desktop/logo1copy.png

The image should be available in the JAR file you create. Then use getResource() to get the image from the jar file. For example,

URL resource = this.getClass().getResource("resources/logo.png");
frame.setIconImage(new ImageIcon(resource).getImage());

Here the logo.png is located under 'resources' folder of the class file where this code is executed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top