Search code examples
javaimagesystem-traytrayicon

Tray Icon displayed not properly


This is my code in my app that works fine:

if (SystemTray.isSupported()) {
    popupMenu = createPopupMenu();
    SystemTray systemTray = SystemTray.getSystemTray();
    Image img = Toolkit.getDefaultToolkit().getImage("image.gif");
    trayIcon = new TrayIcon(img);
    systemTray.add(trayIcon);
    trayIcon.addMouseListener(new SystemTrayMouseListener());
  }

The only problem is that the Tray shows only the upper left quarter of the image. I've tried with different extension and format, but the problem still stand. Is my bad? Are there any solutions?


Solution

  • You should call the trayIcon method setImageAutoSize(true), which will automatically resize the image to be fully displayed as a tray icon.

    if (SystemTray.isSupported()) {
        popupMenu = createPopupMenu();
        SystemTray systemTray = SystemTray.getSystemTray();
        Image img = Toolkit.getDefaultToolkit().getImage("image.gif");
        trayIcon = new TrayIcon(img);
        trayIcon.setImageAutoSize(true);
        systemTray.add(trayIcon);
        trayIcon.addMouseListener(new SystemTrayMouseListener());
    

    }

    Hope it helps! ;)