1

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

Community
  • 1
  • 1
Débora
  • 5,390
  • 21
  • 84
  • 159

2 Answers2

2

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);
            }
        }
    });
Jeffrey
  • 42,428
  • 7
  • 84
  • 138
  • Thanks Jeffrey.It worked well.There was a problem which is the JPopmenu remained even after the mouse left the JPopup.I could comeup with it by adding a mouseListener.(if mouse exited, then JPopup is invisible. :) ). Thanks again – Débora Jan 15 '12 at 05:26
0

SystemTray have gor implemented simple syntax

TrayIcon(Image, "Narrative", JPopupMenu);

there no required add any additional Listener for displaying JPopupMenu

Community
  • 1
  • 1
mKorbel
  • 108,320
  • 17
  • 126
  • 296
  • Umm, no there isn't. http://docs.oracle.com/javase/7/docs/api/java/awt/TrayIcon.html – Jeffrey Jan 14 '12 at 18:05
  • @Jeffrey that isn't reall issue, that about quality of Oracle's Java7 API, and reason why use Sun's Java6 API, in some cases equals suicide to read that, btw J2SE application (<= Java5) hasn't implemented SystemTray or TrayIcon, only via JNI (maybe by implement one Jack Daniels is possible some woodoo with JNA & Runtime.exec()) – mKorbel Jan 14 '12 at 18:32
  • I was commenting on the lack of a `TrayIcon(Image, String, JPopupMenu)` constructor. `TrayIcon`s can only take `PopupMenu`s as of Java 7. – Jeffrey Jan 14 '12 at 23:13
  • Thanks mKorbel.I think that I had to explain more on my question.and I updated.I want to add an image to the `MenuItem` of the popup menu in SystemTray and not Image to the systemTray. – Débora Jan 15 '12 at 04:38
  • are you sure because i can't found what you talking about – imanis_tn Apr 19 '12 at 22:21