3

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?

Marcio Barroso
  • 783
  • 1
  • 7
  • 21

3 Answers3

8

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.

David Lavender
  • 8,021
  • 3
  • 35
  • 55
  • *"Icons must be **exactly** 16x16 and 32x32."* No they don't. I just tried with icons 17x17 & 31x31 and it worked as advertised. Having said that, I don't know what the exact dimensional 'cross-overs' are, and it makes sense to not force the JRE (or the OS) to resize images at run-time by a method that is neither obvious nor controllable. – Andrew Thompson Feb 21 '13 at 16:54
  • I am using Java 1.5 and this method setIconImages() is not available in this version =/ – Marcio Barroso Feb 21 '13 at 17:35
  • 1
    Icon sizes are discussed in [this question](http://stackoverflow.com/questions/18224184/sizes-of-frame-icons-used-in-swing). – pvorb Jun 26 '14 at 12:28
3

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.

Durandal
  • 19,919
  • 4
  • 36
  • 70
2

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.

Marcio Barroso
  • 783
  • 1
  • 7
  • 21