Reputation: 227
I've been trying all day to get my tray icon added, but it doesnt work. I have the icon file stored within the netbeans src/myproject/
I have tried a gazillion different paths, even direct ones to my files, but none seem to work. I'm pretty sure something in my code doesnt work, I simply can't see it.
public void createSystemTrayIcon() {
if (SystemTray.isSupported()) {
SystemTray tray = SystemTray.getSystemTray();
Image img = Toolkit.getDefaultToolkit().getImage("smallicon.ico");
PopupMenu popup = new PopupMenu();
final MenuItem menuExit = new MenuItem("Quit");
MouseListener mouseListener =
new MouseListener() {
public void mouseClicked(MouseEvent e) {
show();
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
};
ActionListener exitListener =
new ActionListener() {
public void actionPerformed(ActionEvent e) {
Runtime r = Runtime.getRuntime();
System.out.println("Exiting...");
r.exit(0);
}
};
menuExit.addActionListener(exitListener);
popup.add(menuExit);
final TrayIcon trayIcon = new TrayIcon(img, "ESOLeaderboards", popup);
ActionListener actionListener =
new ActionListener() {
public void actionPerformed(ActionEvent e) {
trayIcon.displayMessage("ESOLeaderboards ","version: EU 1.0",
TrayIcon.MessageType.INFO);
}
};
trayIcon.setImageAutoSize(true);
trayIcon.addActionListener(actionListener);
trayIcon.addMouseListener(mouseListener);
try {
tray.add(trayIcon);
} catch (AWTException e) {
System.err.println("TrayIcon could not be added.");
}
} else {
// System Tray is not supported
}
}
Upvotes: 1
Views: 4229
Reputation: 4293
In windows 10, I've been able to view a 16 pixels PNG in the tray this way:
final URL resource = getClass().getResource("icon16.png");
final TrayIcon trayIcon = new TrayIcon(Toolkit.getDefaultToolkit().getImage(resource), "Application v0.1 tooltip");
It's "expected" that other OSes would down-scale a big icon, but I haven't tested it.
Upvotes: 1
Reputation: 20059
The standard method of embedding images (and any other non-class resources) is to just put them in a package in the project, e.g. if you project is using com.myproject.myapp
, then create a package (just a folder, really) images
(under myapp) and put you image files there.
Access to those images (resources) is gained by using the ClassLoader
methods getResource(name) and/or getResourceAsStream(name). For simplicity to get the right ClassLoader instance many prefer to also create a class (e.g. "Images") in that same package and define static constants there to access the resources by name (as simple as Images.CONSTANT_NAME). Project structure may look like this:
com.myproject.myapp
images
Images.class
MyImage1.png
In the images class, constants can be defined either for the resource handle or the resources themselves:
public final static URL MY_IMAGE_1 = Images.class.getResource("MyImage1.png");
(The URL could then be passed to Toolkit). If an eager load of everything is desired/feasible the loaded images themselves:
public final static Image MY_IMAGE_1 = loadImage("MyImage1.png");
private static Image loadImage(String name) {
URL url = Images.class.getClassLoader().getResource(name);
Image img = Toolkit.getDefaultToolkit().getImage(url);
// possible hack to force pre-loading of (toolkit) image in next line
// new ImageIcon(img);
return img;
}
Obviously I omitted all error handling from the examples, loading methods should include detailed error handling and reporting (logging/System.out and/or throwing appropiate exceptions) when something goes wrong.
These approaches will work in IDE as well as after creating a jar file for the program.
Upvotes: 0
Reputation: 14471
Toolkit.getDefaultToolkit().getImage("smallicon.ico");
supports only JPG, PNG and GIF images.
It doesn't support ico images. Use another image.
Upvotes: 2