Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 829
Dan Avatar asked Aug 13 '26 07:08

Dan


1 Answers

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.

like image 77
Arnaud Denoyelle Avatar answered Aug 14 '26 21:08

Arnaud Denoyelle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!