Here is an example of how you can create a SystemTray icon in Java:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class SystemTrayIconExample {
public static void main(String[] args) {
if (SystemTray.isSupported()) {
SystemTray systemTray = SystemTray.getSystemTray();
Image image = Toolkit.getDefaultToolkit().getImage("icon.png");
PopupMenu popupMenu = new PopupMenu();
MenuItem exitItem = new MenuItem("Exit");
exitItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
popupMenu.add(exitItem);
TrayIcon trayIcon = new TrayIcon(image, "System Tray Icon", popupMenu);
trayIcon.setImageAutoSize(true);
try {
systemTray.add(trayIcon);
} catch (AWTException e) {
e.printStackTrace();
}
} else {
System.out.println("System Tray is not supported");
}
}
}
In this example, we first check if the system supports the SystemTray. We then create an Image object with the icon image file. We create a PopupMenu with an "Exit" MenuItem that exits the program when clicked. We create a TrayIcon with the image, tooltip text, and PopupMenu. Finally, we add the TrayIcon to the SystemTray.
Make sure to replace "icon.png" with the path to your icon image file.