Sizes of frame icons used in Swing
Asked Answered
A

6

33

We can use a list to initialise the window icons using Window.setIconImages(List<? extends Image>). What are the different sizes of icons typically used for in a JFrame?

Code

This code turns 64 different sized images (from 16x16, incrementing by 2) into icons for the list.

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class FrameIconList {

    public static BufferedImage getImage(int size, Color color) {
        BufferedImage i = new BufferedImage(
                size, size, BufferedImage.TYPE_INT_RGB);

        Graphics2D g = i.createGraphics();

        g.setColor(color);
        g.fillRect(0, 0, size, size);
        g.setColor(Color.BLACK);
        int off = (size>17 ? 3 : 1);
        if (off>1) g.drawRect(0, 0, size-1, size-1);
        g.drawString("" + size, off, size-off);

        g.dispose();

        return i;
    }

    public static void main(String[] args) {
        final Color[] colors = {
            Color.GREEN,
            Color.RED,
            Color.YELLOW,
            Color.WHITE,
            Color.CYAN,
            Color.MAGENTA,
            Color.PINK,
            Color.ORANGE
        };

        int s = 64;
        final int[] sizes = new int[s];
        
        for (int ii=0; ii<sizes.length; ii++) {
            sizes[ii] = 16+(ii*2);
        }

        Runnable r = new Runnable() {

            @Override
            public void run() {
                // the GUI as seen by the user (without frame)
                JPanel gui = new JPanel(new BorderLayout());
                gui.setBorder(new EmptyBorder(2, 3, 2, 3));
                gui.setBackground(Color.WHITE);
                
                ArrayList<BufferedImage> images = new ArrayList<BufferedImage>();
                Vector<ImageIcon> icons = new Vector<ImageIcon>();
                for (int ii=0; ii< sizes.length; ii++) {
                    BufferedImage bi = getImage(
                            sizes[ii], 
                            colors[ii%colors.length]);
                    images.add(bi);
                    ImageIcon imi = new ImageIcon(bi);
                    icons.add(imi);
                }
                JList list = new JList(icons);
                list.setVisibleRowCount(6);
                gui.add(new JScrollPane(list));

                JFrame f = new JFrame("Icon size usage");
                f.setIconImages(images);
                f.add(gui);
                // Ensures JVM closes after frame(s) closed and
                // all non-daemon threads are finished
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                // See https://mcmap.net/q/120823/-how-to-best-position-swing-guis for demo.
                f.setLocationByPlatform(true);

                // ensures the frame is the minimum size it needs to be
                // in order display the components within it
                f.pack();
                // should be done last, to avoid flickering, moving,
                // resizing artifacts.
                f.setVisible(true);
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
        SwingUtilities.invokeLater(r);
    }
}
Auklet answered 14/8, 2013 at 5:56 Comment(0)
A
34

Typical views for this Windows 7 based PC

Note: @bobbel reports the same sizes are used for Windows 10.

Frame - 20x20

Icon Size Usage - showing 20x20 frame icon

Task Bar - 40x40 in task bar itself, hover app shows 20x20

Icon Size Usage - Task Bar

Windows+Tab - 20x20

Icon Size Usage - Windows+Tab

Alt+Tab - 40x40 in lower right, shrunken 20x20 in upper left.

Icon Size Usage - Alt+Tab

Task Manager - 20x20

Icon Size Usage - Task Manager

Auklet answered 14/8, 2013 at 5:56 Comment(2)
Just tested: all the same sizes in Win10, too.Nysa
This is probably with the display scale set to 125%. Windows uses 16×16 icon in the app frame and 32×32 in Alt+Tab when no scaling is in effect (100% scale). This aligns with 16*1.25=20, 32*1.25=40. If the scale is set to 150% in display settings, the icons will be larger: 24 and 48 correspondingly.Artful
C
13

@mKorbel Huh.. did not realize there would be a difference there. Well, user/OS preferences rules over programmer expectation! ;)

  • answer is only about Win8(ent, 64b ....)/WinXP(not mentioned, but setting are quite similair)

  • there are another options please see Bug or feature: Swing default gui font incorrect for Win6+ by @kleopatra, etc

  • is possible to set 64x64 icon on desktop in win8

  • e.g. my setting (not advanced graphics personalizations, despite the fact that I'm Win2008/12 admin, blablabla- "leaving surealism with reversed color scheme, now only with Black & Orange Colors" -end blablabla)

enter image description here

  • generating

enter image description here

  • standard setting (only unmarked Use small taskbar buttons)

enter image description here

  • your window on my screen

enter image description here

Cosmetician answered 14/8, 2013 at 7:15 Comment(0)
C
12

It seems there are no Frame or Dock icons when run in Mac OS X 10.9 (Mavericks):

Lack of Java Swing icons in Mac OS X 10.9 (Mavericks)

Also, no icon in Activity Monitor:

enter image description here

Cribbage answered 21/2, 2014 at 21:18 Comment(2)
Deployment of a Java app on Mac is not as easy as on Windows to let it feel native. The initial steps are wrapping your Java application in an .app directory with an Info.plist descriptor file in it, which in turn points to an icon that can be used for the application. See here.Bandaranaike
@EthanB, the only solution I've found is java -Xdock:name="My App" -Xdock:icon="/path/to/myicon.icns"Commode
T
11

Ubuntu 12.04 LTS

The taskbar icon size can be changed between 32 and 64, but all the time uses the 32x32 icon. I have also recompiled the program, but keeps using the same icon.

Taskbar and window (there is not icon at windows).

enter image description here

Alt + Tab

enter image description here

no icon at task manager

Tera answered 22/2, 2014 at 22:37 Comment(0)
R
4

This is my result on windows 10, it is depending on the Screen scaling:

At 100%: Frame - 16x16 Taskbar - 32x32

At 125% Frame - 20x20 Taskbar - 40x40

At 150% Frame - 24x24 Taskbar - 48x48

At 175% Frame - 28x28 Taskbar - 56x56

At 200% Frame - 32x32 Taskbar - 64x64

Rood answered 15/1, 2021 at 15:50 Comment(3)
This question is 6.5 years old and already has many answers, one of which has been accepted.Reinwald
Yes, and the accepted answer is not correct about Windows 10.Rood
I have found the same thing. Windows 10 (July 2021) uses icons of all these sizes (depending on different scaling settings): 16, 20, 24, 28, 32, 40, 56, 64px. If you don't provide exact icon sizes, Windows will scale up or down whatever icons it finds (although it doesn't do a great job of it).Howe
N
2

I've found out an interesting thing on Win10 (could be also true for Win7 and Win8, but I haven't tried it yet).

By default, Win10 will use image sizes of 20x20 (small) and 40x40 (big).

So, what if you let start your image sizes at 22? It will use image sizes 30x30 (small) and 40x40 (big)!

Generating a whole table of it shows interesting behaviors (tests between tested start sizes result in previous sizes; so 4 will also result in 20x20 and 40x40):

If you start at 2, it will use 20x20 and 40x40.
If you start at 22, it will use 30x30 and 40x40.
If you start at 32, it will use for both 40x40.
If you start at 42, it will use for both 60x60.
If you start at 62, it will use 78x78 and 80x80.
If you start at 80, it will use for both 80x80.
If you start at 82, it will use 98x98 and 120x120.
If you start at 100, it will use 100x100 and 120x120.
If you start at 102, it will use 118x118 and 120x120.
If you start at 120, it will use for both 120x120.
If you start at 122, it will use 138x138 and 158x158.
... okay this is enough ...

I don't really get the pattern behind, but i found it very interesting...

In the end it's really up to you, which sizes you provide. Every OS has it's own logic to show up specific icons. Either it will be scaled up or down if you don't provide the exact image sizes for each OS.

Nysa answered 6/12, 2015 at 22:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.