3
votes

Is there a way to use different icons on JFrame and Windows taskbar?

When I set JFrame.setIconImage(img) this same image is use as Windows icon. Can I use different icons to the JFrame and Windows taskbar?

3
AFAIK, only if the JFrame is iconified, by listening from WindowListenermKorbel
Yes, but the same icon used in JFrame is showed in Windows taskbar. I want to use different icons. Is it possible?Marcio Barroso

3 Answers

8
votes

This work in Windows 7 running standard Java 7:

List<Image> icons = new ArrayList<Image>();
icons.add(new ImageIcon("16.png").getImage());
icons.add(new ImageIcon("32.png").getImage());
f.setIconImages(icons);

Icons must be exactly 16x16 and 32x32.

3
votes

You can use setIconImages() to provide a list of icons in different sizes. The JRE will select the best available size to use for each use (so a more detailed version can be shown when the icon is displayed in a larger size).

AFAIK there is no planned way to show different icons for specific uses.

You could make use of setUndecorated(true) and render the windows decorations yourself, but its non-trivial to make this work as intended (due to look&feel). A hacky solution could be to find your way trough the window peer components (with the JDK source + reflection at runtime) and supply a different icon to one or the other peer component. Again this may require code specific to the active L&F.

2
votes

I cound not use the suggested solutions because I have jdk 1.5 as requirement ...

So, I did this:

public void setAppIcons(JFrame frame) {
    List<Image> images = new ArrayList<Image>();
    images.add(getImage(MessageUtils.getString("application.images.icon.app.32")).getImage());
    images.add(getImage(MessageUtils.getString("application.images.icon.app.16")).getImage());

    try {
        Class<?> [] types = {java.util.List.class};
        Method method = Class.forName("java.awt.Window").getDeclaredMethod("setIconImages", types);

        Object [] parameters = {images};
        method.invoke(frame, parameters);
    } catch (Exception e) {
        frame.setIconImage(images.get(0));
    }       
}

If the client is running the application in a jre 1.6 or major, the application will pick the image list to set ...

Tks for your suggestions.