The Source for Java Technology Collaboration
User: Password:



Alexander Potochkin

Alexander Potochkin's Blog

JTrayIcon update

Posted by alexfromsun on February 14, 2008 at 11:12 AM | Comments (18)

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

JXTrayIcon with the current JDK 6

the same code is run under JDK 6 update 10 or JDK 7

JXTrayIcon with JDK 6u10 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.

alexp


Bookmark blog post: del.icio.us del.icio.us Digg Digg DZone DZone Furl Furl Reddit Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment

  • Alex, there seems to be contradictory statements about JPopupMenu on tray icons coming from the Swing team. What about Artem's post from 2006 that shows how to do it? And it looks like you'll have yet another scenario to check in the next Windows version. At least the first milestone of Windows 7 changed the way the hidden tray icons are operated.

    Posted by: kirillcool on February 14, 2008 at 11:51 AM

  • Hello Kirill

    Actually it was me who Artem consulted with before he wrote that blog
    (the jpopup.setInvoker(jpopup) hack)
    I see no contradictions here, take the screenshot from the Artem's blog
    you can see there that popup menu doesn't overlap the taskbar like all system popups do
    and ClassCastException was somewhere under the table, just have a look at the comments

    Thanks for the information about the Windows 7

    alexp

    Posted by: alexfromsun on February 14, 2008 at 12:58 PM

  • Hi! thanx for article, already had same problem, but... Method, described by Artem, didn't worked on my Linux box (menu showed but never hided until you implement some additions to listener, but it is not a way). I haven't already tested, but it seems to me that JDIC project already solved this problem. If you download JDIC source package (from jdic.dev.java.net) you may look at WinTrayIconService and GnomeTrayIconService classes, both of them have method setPopupMenu with popup menu listeners, and this listeners do all the job (and looks similar enough) of handling JPopupMenu. The only problem i see - i can't understand using of sun.* packages and any need of them. May be we can unite our forces to implement TrayIcon implementation that will rule? :) PS: another problem i see - KDE supports transparency for System Tray, and the only Java tray implementation that supports it - Qt Jambi (http://doc.trolltech.com/qtjambi-4.3.2_01/com/trolltech/qt/qtjambi-systemtrayexample.html)

    Posted by: paranoid1024 on February 15, 2008 at 01:28 AM

  • oops, sorry for no formatting - my fault...

    Posted by: paranoid1024 on February 15, 2008 at 01:29 AM

  • Thanks for working on this Alex!

    Posted by: cowwoc on February 15, 2008 at 07:08 AM

  • Under Vista 64-bit I notice that the pop-up menu always displays to the left of the tray icon while both the Java pop-up and Thunderbird one display to the right.

    Is there some OS-level property we should be reading which indicates which direction the menus should pop up and we're forgetting to do this?

    Posted by: cowwoc on February 15, 2008 at 07:11 AM

  • Hello cowwoc

    Does it happen because Java and Thunderbird tray icons
    are placed on the left side of your tray area
    and they have enough room to show their popups
    when the other popups can't be shown at right side of the click ?

    Thanks
    alexp

    Posted by: alexfromsun on February 15, 2008 at 07:34 AM

  • We've implemented the tray icon support into our latest Yakkle release (www.yakkle.com) and since our application needs to inform our users about incoming instant messages or incoming emails or contacts coming online, it has worked like a champ! Is there a way to "bold" one of the entries in the tray popup menu? I notice that it seems the "default" operation is bolded for Windows apps that use tray icons and I'd like to have that behavior here.

    Posted by: ericzenviva on February 15, 2008 at 06:27 PM

  • I get this Exception if I try the tray.jar under ubuntu 7.10.
    Exception in thread "AWT-EventQueue-0" java.lang.UnsupportedOperationException
            at java.awt.TrayIcon.(TrayIcon.java:105)
            at java.awt.TrayIcon.(TrayIcon.java:129)
            at org.jdesktop.swinghelper.tray.JXTrayIcon.(Unknown Source)
            at org.jdesktop.swinghelper.tray.JXTrayIcon.createGui(Unknown Source)
            at org.jdesktop.swinghelper.tray.JXTrayIcon.access$200(Unknown Source)
            at org.jdesktop.swinghelper.tray.JXTrayIcon$3.run(Unknown Source)
            at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
            at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
            at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
            at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
            at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
            at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
    
    java version "1.6.0_03"
    Java(TM) SE Runtime Environment (build 1.6.0_03-b05)

    works fine on XP with update N installed very good!

    I am looking forward to see the JXTrayIcon in the next SwingX release ;-)

    Posted by: mbien on February 16, 2008 at 11:29 AM

  • Observations from Windows: 1. Some native apps display a different menu on right and left click, some display the same menu on both, and some display only one menu on right click. 2. Some applications display one (anywhere in the menu) item highlighted/bold. This item is the default item, and is executed with a double click on the icon, rather than selection in the menu. Might be a good idea to take this into account, combined with the possibility og having two different menu's, from point 1.

    Posted by: lurk_3245 on February 16, 2008 at 04:23 PM

  • Hello everybody

    to ericzenviva
    with Swing menu it is certainly possible to set the bold font for a menuItem,
    see JMenuItem.setFont()

    to mbien
    This exception is thrown by AWT, I think it is also fixed for 6u10,
    see bug 6438179

    to lurk_3245
    The idea behind JXTrayIcon is to provide basic functionality
    it shouldn't be a problem to customize it for particular application

    to kermitas
    I didn't know about this issue with awt popups
    I agree with you - it would look really strange for programms with animation

    Thank you !
    alexp

    Posted by: alexfromsun on February 18, 2008 at 07:01 AM

  • I tested again with Java SE update N build 8 and 12 on ubuntu 7.10 and it worked this time. Good work with the bug fixes!

    The only glitch I noticed is that the tray Popup is listed as regular window in the task bar - probably another linux related bug.

    Posted by: mbien on February 27, 2008 at 03:28 AM

  • Few feedback using JXTrayIcon + Kubuntu 7.10 + KDE+ Sun JRE 1.6.0 u5:

    * There's still the bug that I encountered in JDIC : the popup is only flashing (except if clicking on the right side of the tray icon) https://jdic.dev.java.net/issues/show_bug.cgi?id=401 To avoid the popup closing, you have to release the mouse over it only
    A working workaround (for jdic) is given here : https://jdic.dev.java.net/issues/show_bug.cgi?id=155

    * Sometimes, when right clicking on the tray icon, then releasing the click over an item fire the action

    * With jdic, a cool feature was the fact, under Linux only however to be allowed to add HTML code in trayicon tooltips and ballon (we displayed images and rich text), is there a way to achieve this in Mustang ?

    * It's a pity we still have the gray rectangle behind the tray under Linux. Tip: use shadows to make it less... gray

    * Thanks for this article and JXTrayIcon, it works like a charm and saved me a lot of time

    Posted by: bflorat on March 28, 2008 at 03:25 AM

  • This is cool. I had decided to use JDIC's TrayIcon, but (a) it requires native libraries that don't ship with the JRE, and (b) Gnome support is broken in 0.9.4, there is a function missing out of libtray.so, and no one has responded to my forum post inquiring about the problem (probably ought to go file a bug report too)... However, I use a third-party L&F in my app, making the awt Popup Menu unusable. So... this blog entry was a godsend for me!

    But I notice that the ClassCastException still occurs in some cases. It only happens if I right-click the tray icon while the menu is visible, and it doesn't happen consistently. I grabbed the JXTrayIcon source and am trying to figure out if there's a way to fix the problem.

    Posted by: sjsobol on April 25, 2008 at 08:37 PM

  • One other thing: you probably meant to name the method getJPopupMenu, not getJPopuMenu

    Posted by: sjsobol on April 25, 2008 at 08:49 PM

  • Hello Sjsobol

    I also found this one more mean ClassCastException
    bug 6690791 will be fixed soon

    getJPopuMenu is another good catch, fixed

    Thank you!
    alexp

    Posted by: alexfromsun on April 28, 2008 at 04:48 AM

  • Hello bflorat

    Thanks for the informative comments !
    We are working on making trayIcons more useful

    Thanks
    alexp

    Posted by: alexfromsun on April 28, 2008 at 04:55 AM



Only logged in users may post comments. Login Here.


Powered by
Movable Type 3.01D
 Feed java.net RSS Feeds