Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Take Charge of Desktop Integration with Java SE 6 : Page 4

With the release of the Java Standard Edition 6.0 Beta, Java developers no longer need to contend with clamp-on solutions or Java Native Interface (JNI) to make their applications interact with desktop products and features. These desktop-integration features now are part of the core.


advertisement

System Tray Icons

The system tray is referred to as the Task Status Area in Windows or the Notification Area in Gnome. It is a nifty little area at the corner of an operating system's desktop UI that can provide visual and direct access to all the running applications. The GoMustang application on the Windows platform allows access with a right mouse click on the system tray icon (see Figure 4).

Click to enlarge

Figure 4. GoMustang's Popup Menu in the System Tray

The GoMustang application SystemTray icon has a popup menu. The popup menu has two menu items: Exit and Launch Browser. As their names suggest, Exit exits the application and Launch Browser opens the default Internet browser and points to http://www.devx.com.



In the past, creating Java applications that played nice with the operating system's desktop system tray required libraries that were not part of core Java. Of the plethora of tools out there, JDIC (Java Desktop Integration Components), an effort developed by a community of developers at javadesktop.org and sponsored by Sun, was at the forefront of desktop integration. Sun made a good decision in including some components of JDIC as part of Java SE 6. The related classes java.awt.SystemTray and java.awt.Desktop represent these key components. Java.awt.SystemTray represents the operating Task Status Area or Notification Area (whichever the case may be).

The icon displayed in the system tray is represented by java.awt.TrayIcon. TrayIcon represents not only the image but also the actual objects that receive events, show tooltips, add popup menu items, display messages, etc. You can set messages and tooltips on TrayIcon via the displayMessage(String) and setToolTip(String) functions that hang off TrayIcon. TrayIcon can also direct ActionEvents to ActionListeners that have been registered via TrayIcon's addActionListener(ActionListener) function. Popup menus are represented by java.awt.PopupMenu, and you can add them to TrayIcon via—you guessed it—addPopupMenu(PopupMenu).

Let's dissect the GoMustang Java class's createTrayIcon() function to see how all of these objects fit to gather. As the heavily edited GoMustang Java code in Listing 1 shows, you first check whether the minimum SystemTray functionality is supported by the operating system by calling SystemTray.isSupported(), which should return true for operating systems supporting it (just in case you were working on DOS!).

Once the program knows the operating system supports the system tray capability, it creates Exit and Launch Browser menu items. These menu items will be added to the popup menu. This menu pops up when the user right clicks on the icon in the system tray. ActionListeners are added to both the Exit and Launch Browser menu items. In-line overriding of actionPerformed(ActionEvent) will give the program an opportunity to perform actions when the user selects these menu items, which for Exit will be exiting the application by issuing System.exit(0) and for Launch Browser will be launching the default browser that points to http://www.devx.com. Launching the browser requires using the new java.awt.Desktop object (more on this in a moment).

The application then creates a popup menu object and adds Exit and Launch Browser menu items to the popup menu. The next step is to create a tray icon with an image of a galloping horse. The gomustang.gif file is bundled in the downloadable JAR file, which is accessed using getClass().getResourceAsStream(file name) and loaded into an image object. The tray icon object is then created with the popup menu and image retrieved from the JAR file. Action and mouse listeners are added to the tray icon, which display a message and do nothing of value respectively for this application. The tray icon is finally added to the system tray. System tray is a singleton object that is retrieved using getSystemTray() on the SystemTray object. That pretty much creates the tray icon in the system tray.

Now let's delve a little deeper to see how the Launch Browser menu item launches the browser. Expanding the inline overridden action listener function actionPerformed(ActionEvent) for the Launch Browser menu item shows how the Desktop object is used (see Listing 2 below):

... MenuItem launchBrowserItem = new MenuItem( "Launch Browser"); ActionListener launchListener = new ActionListener() { public void actionPerformed(ActionEvent e) { if (Desktop.isDesktopSupported()) { actionMessage = "Launched Browser"; try { trayIcon.displayMessage("GoMustang!", "Launching Browser...", TrayIcon.MessageType.INFO); Desktop desktop = Desktop.getDesktop(); desktop.browse(new URI("http://www.devx.com")); trayIcon.setToolTip("GoMustang!"); } catch(Exception exp){...} } } }; launchBrowserItem.addActionListener(launchListener); ...

The key object of interest in Listing 2 is the java.awt.Desktop class. The Desktop class, as mentioned earlier, came from JDIC. The Desktop class relies entirely on the operating system's native mappings of file types to specific desktop applications and its default settings for the Internet browser and email client to locate and launch the desktop application. The default mapping for the Internet browser application is Mozilla's FireFox, so the desktop.browse(URI) function opens a browser window pointed to http://www.devx.com after displaying the message "Launching Browser...".

More on the Way for Control Freaks

Things are just staring to get interesting in the desktop space for Java. I expect to see much more control of the desktop in coming Java releases.



Gautam Shah is a solutions architect with Unisys. He has been developing large, complex applications for over a decade, including solutions that enable information sharing between government entities. He has varied expertise in J2EE, .NET, open source solutions, and various integration platforms such as BizTalk, WebMethods, and WebSphere Business Integration Server.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap