javaimageswingpackagingsystem-tray

Change System tray Icon in java


I just want to change the system tray Icon image for my application. I did 2 things -

Just changed the URL in the default program -

final TrayIcon trayIcon = new TrayIcon(createImage("images/Graph.png", "tray icon"));

Second try -

Image img = Toolkit.getDefaultToolkit().getImage("images/Graph.png");
final TrayIcon trayIcon = new TrayIcon(img, "Application Name", popup);

The application launches in both cases but no image is shown. Its a blank placeholder. What am i doing wrong ?


Solution

  • images/Graph.png is not a valid URL for an image located in your jar. Hence, I guess that img is null on your second try.

    I suggest you this way :

    //Get the URL with method class.getResource("/path/to/image.png")
    URL url = System.class.getResource("/images/Graph.png");
    
    //Use it to get the image
    Image img = Toolkit.getDefaultToolkit().getImage(url);
    
    final TrayIcon trayIcon = new TrayIcon(img, "Application Name", popup);
    

    You shall also ensure that images/ is in your classpath.