Posted by alexfromsun
on February 14, 2008 at 11:12 AM PST
TrayIcon and JPopupMenu - will they ever be friend?
JDK 6 introduced plenty of cool desktop features, like famous gray-rect fix or advanced drag and drop support ,
all of them made java much closer to the desktop application market and I am very exited about them.
There have been many blogs how useful new features are,
and I don't want to repeat them. Among all successful and well-designed features there is one which actually has not been completed,
I am speaking about JTrayIcon.
Swing and AWT team work together (by the way, AWT team seats in 10 steps from my room), AWT is responsible for the "native" parts of the code, like topLevel frames, event processing etc,
when Swing team if focused on their lightWeight components. Actually it is AWT who isolates Swing from any native code and makes Swing cross-platform.
It wasn't a surprise that TrayIcon feature came to AWT team, and they implemented it perfectly - it works well and it is supported for various Windows, Linux and Solaris systems.
They made it customizable by providing mouse event handling support and a special method setPopupMenu() which takes java.awt.Popup (which is quite old-fashioned but remember it was the AWT feature)
JPopupMenu doesn't extend awt.Popup but it looked like it would be easy to attach JPopupMenu to the trayIcon with help of MouseListener, but it was not the case.
The fact is that there is no way (or at least easy one) to correctly attach JPopupMenu to a TrayIcon.
It means that you can't set an icon for a TrayIcon's menuItem or make it look according to the current Look and Feel and, from my point of view, it makes TrayIcon much less useful than it could be.
It is our fault, sorry about that. Now let's see how we can solve this problem.
To make JTrayIcon possible I fixed three particular bugs for the next JDK 6 update release (the famous update #10, aka 6uN) and for the JDK 7 beta
ClassCastException in Swing with TrayIcon
If JPopupMenu is shown from a trayIcon's mouseListener an ugly ClassCastException was thrown in some cases.
Keyboard navigation problems and highlighting of the first item
If popup menu lost focus it stopped processing keyboard events in some cases, the first item a popup was highlighed by default
which is very different from any other GUI I've seen
Swing Popups should overlap taskbar
Finally it was impossible to implement a good looking trayicon's popup menu, because Swing popups couldn't overlap taskbar
Having integrated all three fixes I managed to write the JXTrayIcon class
which extends java.awt.TrayIcon and contains
setJPopuMenu(JPopupMenu ) method
Here is how JXTrayIcon looks when you click on it, with the current JDK 6
the same code is run under JDK 6 update 10 or JDK 7
The red quarter circle icon is owned by the JXTrayIcon's test.
Note that the first item is not highlighted by default when the popup menu is shown,
if you move the mouse over it or press the arrow button on your keyboard, the proper item will be highlighted as with a system popup menu.
I encourage you to download the JDK 6u10 early bits and try out the JXTrayIcon demo
The runnable jar file is also available for your convenience
I particularly interested how it works in different environments,
any comments and suggestions are welcome as usual
Thank you and sorry this took so long.I hope it was worth the wait.