I read the system tray tutorial and this similar Stack Overflow question but can't find a good answer. I want to add an image to menu item in J2SE application. In the tutorial, MenuItem is used, but I couldn't find how to add icons to menu items in SystemTray pop up. If JMenuItem is used, icons can easily be placed in MenuItems, but there is MenuItem. How can I add an image to my system tray popmenu?
UpdatedHere, I want to add an image to MenuItem in the popup menu(not to the SystemTray.)
You can use a JPopupMenu with your TrayIcon (read here).
trayIcon.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger()) {
jpopup.setLocation(e.getX(), e.getY());
jpopup.setInvoker(jpopup);
jpopup.setVisible(true);
}
}
});
SystemTray have gor implemented simple syntax
TrayIcon(Image, "Narrative", JPopupMenu);
there no required add any additional Listener for displaying JPopupMenu
Related
I ran into an Issue with Java awt tray icons under Ubuntu Gnome 16.04:
The Icon is displayed in the top left corner of my screen and in the System Tray appears a black square. The MouseListener is also not working (neither on the icon nor on the black square).
Here is my Code:
if (SystemTray.isSupported()) {
Image image = ImageIO.read(EyeUNIFYlocal.class.getResource("/star.png"));
TrayIcon trayIcon = new TrayIcon(image);
trayIcon.setImageAutoSize(true);
trayIcon.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
System.out.println("Clicked");
}
});
try {
SystemTray.getSystemTray().add(trayIcon);
} catch (AWTException ex) {
System.err.println("Error while creating tray icon.");
}
} else {
System.err.println("Tray icons are not supported on this System.");
}
This code works fine on Windows 10.
Thank you in advance!
Java System Tray support doesn't exist for newer Linux distributions, mostly because of the changes from GtkStatusIcon to AppIndicator, and GTK2/3 changes as well (so, issues with JavaFX unless you install some additional libraries).
Additionally, since you mentioned Gnome -- Gnome likes to "hide" the AppIndicator as "notifications", so there is an extension (top-icons) that lets you restore the indicators back to the top of the screen (instead of in a hidden drawer at the bottom left of the screen)
If you want to display cross-platform system tray icons, I suggest the SystemTray project. There is an inbound 3.0 release soon (an API rewrite and better native support), but the older 2.x version should solve the problem you are having.
I'm trying to develop a Mac OsX app provided by a system tray icon, so after the first attempt with the simplest code to achieve it I noticed that every apps tray icon's (both system and user apps) on mac osX (10.8) allows to activate the relative popup menu with both left and right click on it but with my project only the left (MouseEvent.BOTTON1) button causes the popup menu to pulldown. Here's my code:
public class SystemTrayDemo
{
private SystemTray tray;
private TrayIcon tray_icon;
public SystemTrayDemo()
{
if (!SystemTray.isSupported())
{
JOptionPane.showMessageDialog(null, "System tray not supported!");
return;
}
else
tray = SystemTray.getSystemTray();
final PopupMenu popup = new PopupMenu();
MenuItem exit = new MenuItem("Exit");
exit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if (tray != null)
{
tray.remove(tray_icon);
System.exit(0);
}
}
});
popup.add(exit);
//add tray icon
tray_icon = new TrayIcon(getIcon("images/wifi.png"), "Open documents...", popup);
tray_icon.setImageAutoSize(true);
try
{
tray.add(tray_icon); // adds icon
}
catch (AWTException ex) {}
}
private Image getIcon(String name)
{
URL _url = getClass().getResource(name);
return new ImageIcon(_url).getImage();
}
public static void main(String args[])
{
new SystemTrayDemo();
}
}
but how I already said, only through left mouse button click.
So during a further attempt I've tried to mimic the behavior of the tray icons of every other apps using a MouseListener and firing a left button event on right click event using dispatchEvent() method like so:
public static void fireMouseEvent(Component c)
{
MouseEvent me = new MouseEvent(c, // which
MouseEvent.MOUSE_CLICKED, // what
System.currentTimeMillis(), // when
MouseEvent.BUTTON1_MASK, // no modifiers
0, 0, // where: at (10, 10}
1, // only 1 click
true); // popup trigger
c.dispatchEvent(me);
}
the event will handled by the mouse listener but obviously TrayIcon Class is not a Component subclass and therefore the source of MouseEvent is null and I get a NPE. Here's my MouseListener:
class MouseAdapt extends java.awt.event.MouseAdapter
{
public void mouseClicked(java.awt.event.MouseEvent me)
{
int button = me.getButton();
if(button == java.awt.event.MouseEvent.BUTTON3)
{
fireMouseEvent(me.getComponent());
}
}
}
try
{
tray.add(tray_icon); // aggiungi l'icona
tray_icon.addMouseListener(new MouseAdapt());
}
catch (AWTException ex) {}
Sorry for my english, I hope that someone who have ever had some experience with that kind of projects can help me. I've searched for hours but with no luck. Thank You for your help.
Edit: There's now a library working to fix all of this here: https://github.com/dorkbox/SystemTray
to activate the [TrayIcon] relative popup menu with both left and right click
This is simply not possible on Mac + Java currently. Using reflection to invoke the underlying triggers doesn't seem to help. This is a bug.
https://bugs.openjdk.java.net/browse/JDK-8041890
only the left (MouseEvent.BOTTON1) button causes the popup menu to pulldown. Here's my code
Even this is broken in some Java versions (7u79), fixed with an upgrade...
https://bugs.openjdk.java.net/browse/JDK-7158615
Cross-Platform TrayIcon Support:
Albeit slightly off-topic, I wanted to add, some projects use a JXTrayIcon to accomplish some fancy drop-down menus in Linux/Windows, etc. These also cause problems on Mac despite a click-bug it already suffers from today as well as bugs with Gnome3 requiring a completely separate hack. But on Mac, any attempt to use the decorated menus causes the menu to linger and is a very bad experience for the end-user. The solution I settled on was to use AWT for Mac, Swing for everything else. The Java TrayIcon support is in dire need of a rewrite. JavaFX claims to help this initiative, but it's staged for Java 9. In the mean time, I'm sticking to OS-dependent hacks.
Related Tray Issues for Other Platforms
Furthermore, some Linux distributions like Ubuntu have removed the tray icon by default in the Unity desktop, causing further headaches. https://askubuntu.com/a/457212/412004
In addition, the transparency of the icon is replaced with a gray color on Gtk/Gnome or Qt/KDE powered desktops (Both OpenJDK and Oracle JRE suffer this)
https://stackoverflow.com/a/3882028/3196753
http://bugs.java.com/bugdatabase/view_bug.do?bug_id=6453521
In addition, Gnome3 powered desktops may show it in the wrong corner, not at all, or it may show but be unclickable (Both OpenJDK and Oracle JRE suffer this)
https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=660157
https://bugzilla.redhat.com/show_bug.cgi?id=1014448
In addition to that, high-DPI screens on Windows have a bug that draws the icon incorrectly: Windows 8 Distorts my TrayIcon
So in summary, the state of the System Tray in Java is OK, but due to the combination of factors is quite fragmented and buggy in JDK6, JDK7 and JDK8.
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)
You could use MouseListener, ie:
trayIcon.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 1) {
}
}
});
See How to Write a Mouse Listener for more details.
EDIT: ActionListener vs MouseListener
There is a concept of low level and semantic events. Whenever possible, you should listen for semantic events rather than low-level events, such as listening for action events, rather than mouse events. Read for more details in Low-Level Events and Semantic Events.
In this case you just need more details from the event so using MouseListener is required.
When I set setAccelerator() to Control + A or Control + P and I run the program it doesn't detect the keystroke.
Here's the code:
menuItem = new JMenuItem("About");
menuItem.setActionCommand("About");
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, Event.CTRL_MASK));
menuItem.setMnemonic(KeyEvent.VK_A);
menuItem.addActionListener(this);
menu.add(menuItem);
Then when it's pressed it should invoke the Action Listener:
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("About")) {
System.out.println("About");
}
}
I'm running it in Eclipse on a Mac if that matters.
Control-A and Control-P are both keystrokes that may already be intercepted, depending on your platform and depending on what has keyboard focus. Control-A may already be intercepted and interpreted as "select all", and Control-P may already be intercepted and interpreted as "paste".
What if you select a less commonly-used keystroke instead of "Control-A", such as "Control-Shift-A" or "Control-B"? Here's a modified version of your code that uses Control-Shift-A instead of Control-A:
menuItem = new JMenuItem("About");
menuItem.setActionCommand("About");
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, Event.CTRL_MASK | Event.SHIFT_MASK));
menuItem.setMnemonic(KeyEvent.VK_A);
menuItem.addActionListener(this);
menu.add(menuItem);
I tested this change on my own system using the JMenu demo from the Swing tutorial, and I found (exactly as you did) that registering Control-A as the accelerator had no effect. However, registering Control-Shift-A as the accelerator worked perfectly.
not sure if it will help, but you're using Event.CTRL_MASK instead of KeyEvent.CTRL_MASK
So i have made a simple program with a basic menu at the top of the frame, Now i just need to put actions behind each JMenuItem. Im struggling to work the code out though, Here is what i thought would work:
JMenu file_Menu = new JMenu("File");
JMenuItem fileExit = new JMenuItem("Exit Program");
file_Menu.add(fileExit);
fileExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
JFrame hello = new JFrame("POPUP");
hello.setSize(100,75);
hello.setDefaultCloseOperation(hello.EXIT_ON_CLOSE);
hello.setVisible(true);
}
});
main_Menu.add(file_Menu);
This doesn't seem to work though, I thought that this code would create a small popup window when the menu item is clicked.
Can any spot the bug because i cant seem to.
Suggestion: Instead of adding a separate ActionListener, just use AbstractAction:
JMenuItem fileExit = new JMenuItem(new AbstractAction("Exit Program") {
public void actionPerformed(ActionEvent ae) {
JFrame hello = new JFrame("POPUP");
hello.setSize(100,75);
hello.setDefaultCloseOperation(hello.EXIT_ON_CLOSE);
hello.setVisible(true);
}
});
I'd also suggest, instead of setting EXIT_ON_CLOSE on the popup menu, you set it on the main frame of your application, and have the action simply call theMainFrame.dispose().
You got it working, but you have another problem.
Don't do this:
hello.setDefaultCloseOperation(hello.EXIT_ON_CLOSE);
When you close the pop-up frame, your entire JVM terminates. Consult JFrame.setDefaultCloseOperation javadocs for a more appropriate value.
Give an instance of Action (extend from AbstractAction) to JMenuItem
Based on the code you posted it looks like it should work, but we can't see the entire context of how the menu item is being used.
Did you debug your code (with a System.out.println) to see if the ActionListener is being invoked?
If you need more help post your SSCCE that demonstrates the problem.
Fixed it.
Forgot to add the actionPerformed method.