Posted by midnightjava
on February 10, 2009 at 12:54 PM PST
I've found it necessary to choose between the JDK and JDIC implementations of system tray functionality at run-time, depending on the version of the JVM in which my code is running. This is because there's a significant bug in the JDIC system tray implementation for Java 6 on Windows (see http://forums.java.net/jive/message.jspa?messageID=273514 ), and of course the JDK doesn't have system tray support prior to Java 6.
I wrote some code that encapsulates this variability and enables a client to abstractly instantiate a system tray icon and make changes to its popup menu, without dealing with the two underlying implementations. The JDK implementation is chosen for Java 6 or later (for future compatibility), and the JDIC implementation is chosen for versions prior to Java 6.
I encapsulated the popup menu and menu item components as well as the system tray and tray icon components, since awt and swing components are used respectively by the JDK and JDIC system tray implementations. The code includes an interface for a tray control object, JDK and JDIC-based implementations of the interface, and a factory class that returns the appropriate implementation for the JVM version. There's also an enum class that defines all the menu items abstractly, so the client can work with them independent of the implementation. I coded the enum class to represent menu items needed for the demo program that's also included. It can be adapted for other uses by defining fields that represent the desired menu items.
The code is available at http://midnightjava.net . Just click on the "Source Code" link, and then click the link for "Tray Control".
Message was edited by: midnightjava