Question

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 ?

Était-ce utile?

La 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.

Autres conseils

The problem is the way you include the image file as the image is inside your . jar, use getResource() or getResourceAsStream, try this:

 try {
    InputStream inputStream= ClassLoader.getSystemClassLoader().getResourceAsStream("/images/Graph.png");
//or getResourceAsStream("/images/Graph.png"); also returns inputstream

  BufferedImage img = ImageIO.read(inputStream);
    final TrayIcon trayIcon = new TrayIcon(img, "Application Name", popup);
}
   catch (IOException e) {}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top