I'm working on a simple Java swing app, which adds an icon to the system tray when created. What I'm trying to do is to detect when this icon is single clicked by the user (whether through left click or right click), There's no popup menu, I just want the app to be restored when the icon is clicked.
This is the code I'm using:
SystemTray tray = SystemTray.getSystemTray();
Image icon = toolkit.getImage("icon.png");
ActionListener listener = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println("click detected");
}
};
TrayIcon trayIcon = new TrayIcon(icon, "Test Program", null);
trayIcon.addActionListener(listener);
tray.add(trayIcon);
What happens when I run this program though, is that single clicks (either left or right) have no effect, but when I double click, then it shows the message 'click detected' in the console.
What can I do to have single clicks also be detected? Do I need to use a MouseListener for this? ( I've heard that MouseListeners can cause problems, and ActionListeners are better)