Carol.Kar
Carol.Kar

Reputation: 5345

Changing the launch icon

I would like to replace the java launch icon:

enter image description here

I am starting my application via spring and put my icon here inside:

enter image description here

I am starting my gui like that my MainWindow extends a JFrame:

/**
 * starts the GUI
 */
public void start() {               
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            Toolkit.getDefaultToolkit().getImage(MainWindow.class.getResource("icon.png"));
            makeLayout();   
        }
    });
}

However the icon does not change. Any recommendation what I could do?

I appreciate your answer!

Upvotes: 0

Views: 101

Answers (3)

martinez314
martinez314

Reputation: 12332

Toolkit.getDefaultToolkit().getImage() returns an image, but you are not doing anything with it. You need to use setIconImage(). Try this...

setIconImage(Toolkit.getDefaultToolkit().getImage(
    MainWindow.class.getResource("icon.png")));

Upvotes: 2

Elmar Okanovic
Elmar Okanovic

Reputation: 9

I believe your path needs to include the "/resources/" part. so it would look like this:

Toolkit.getDefaultToolkit().getImage(MainWindow.class.getResource("resources/icon.png"));

Upvotes: 0

Niru
Niru

Reputation: 742

URL url = new URL(path);
Toolkit kit = Toolkit.getDefaultToolkit();
Image img = kit.createImage(url);
getFrame().setIconImage(img);

The URL can throw MalformedUrlException. If so happens, then just replace the first line with this

URL url = ClassLoader.getSystemResource(path);

Upvotes: 0

Related Questions