Search code examples
javaswingsystem-trayjpopupmenu

System Tray Pop up menu item with Image(Icon to the MenuItem)


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


Solution

  • 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);
                }
            }
        });