java: TrayIcon right click disabled on Mac OsX -


i'm trying develop mac osx app provided system tray icon, after first attempt simplest code achieve noticed every apps tray icon's (both system , user apps) on mac osx (10.8) allows activate relative popup menu both left , right click on project left (mouseevent.botton1) button causes popup menu pulldown. here's 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 said, through left mouse button click. during further attempt i've tried mimic behavior of tray icons of every other apps using mouselistener , firing left button event on right click event using dispatchevent() method so:

    public static void firemouseevent(component c)     {         mouseevent me = new mouseevent(c, //                     mouseevent.mouse_clicked, //                     system.currenttimemillis(), // when                     mouseevent.button1_mask, // no modifiers                     0, 0, // where: @ (10, 10}                     1, // 1 click                      true); // popup trigger          c.dispatchevent(me);     } 

the event handled mouse listener trayicon class not component subclass , therefore source of mouseevent null , npe. here's 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 english, hope have ever had experience kind of projects can me. i've searched hours no luck. thank help.

edit: there's library working fix of here: https://github.com/dorkbox/systemtray


to activate [trayicon] relative popup menu both left , right click

this not possible on mac + java currently. using reflection invoke underlying triggers doesn't seem help. bug.

https://bugs.openjdk.java.net/browse/jdk-8041890

only left (mouseevent.botton1) button causes popup menu pulldown. here's code

even broken in java versions (7u79), fixed upgrade...

https://bugs.openjdk.java.net/browse/jdk-7158615

cross-platform trayicon support:

albeit off-topic, wanted add, projects use jxtrayicon accomplish fancy drop-down menus in linux/windows, etc. these cause problems on mac despite click-bug suffers today bugs with gnome3 requiring separate hack. on mac, attempt use decorated menus causes menu linger , bad experience end-user. solution settled on use awt mac, swing else. java trayicon support in dire need of rewrite. javafx claims initiative, it's staged java 9. in mean time, i'm sticking os-dependent hacks.

related tray issues other platforms

furthermore, linux distributions ubuntu have removed tray icon default in unity desktop, causing further headaches. https://askubuntu.com/a/457212/412004

in addition, transparency of icon replaced gray color on gtk/gnome or qt/kde powered desktops (both openjdk , 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 in wrong corner, not @ all, or may show unclickable (both openjdk , 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 that, high-dpi screens on windows have bug draws icon incorrectly: windows 8 distorts trayicon

so in summary, state of system tray in java ok, due combination of factors quite fragmented , buggy in jdk6, jdk7 , jdk8.


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -