Thrilling Desktop APIs in JDK 1.6
Posted On January 5, 2008 by Sneha Latha filed under Java
This article introduces some of the most thrilling new APIs provided in Java Development Kit (JDK) 1.6 for seamless integration with the native desktop environments, whether it is Windows, or Linux, GNOME, or KDE.
Introduction
Java language is well known for its platform independence, i.e., a program written, and compiled on one system is guaranteed to provide the same performance on any other system irrespective of the fact that what the processor is, and what the OS is. Only requirement is that Java Runtime Environment (JRE) should be available in that system. This strength of Java has made it shy away from things, which are too specific to a platform. As of JDK version 1.6, some of the most intriguing problems related to desktop integration are removed.
Java developers can display a smart splash screen, while their application loads on the background (just the way, the major software suites do). Java developers can launch Excel spreadsheets, and Word documents from within their program (just as if the file has been opened by clicking on it). This is really thrilling for a Java solution provider because these facilities are available not only on Windows but also on all Java supported operating systems.
This article explains the usage of essential desktop functionalities provided in version 1.6 of JDK; i) displaying splash screen, ii) opening a file for editing, printing, and activating default mailer program to send an email.
Displaying Splash Screen
All applications worth their name display a welcome screen (splash screen), when they start up. But this facility was not directly built into Java until JDK 1.6 arrived. java.awt.SplashScreen class allows display of splash images, and drawing anywhere on the image. The getSize() method is used to get the width, and the height of the image window. If we want to draw some custom text information over the image, the Graphics2D object associated with the image can be obtained by calling createGraphics() method.
Currently, the displayed image can be changed by calling setImageURL(java.net.URL) method. The sample code listed below creates a splash screen having an ant as the initial image. After 5 seconds, the image is changed into an artistic sun. (Both these images have been taken from Swingset2 demo of Java Development Kit). A countdown appears at the bottom of the images with the message Not a rocket launch: followed by a count down.
The actual window appears after 10 seconds, since we deliberately create a delay by calling sleep method of Thread class. This is not the case in real world applications. The splash screen is displayed just until the window is assembled, and displayed. Hence, it is better to omit the sleeping part of displayCountDown method when using splash screen display for a serious project.
| .................................................................................................................................................................. import java.awt.*; import javax.swing.*; public class SplashScreenDisplay extends JFrame { public SplashScreenDisplay() { super("Just a Test of Splash"); this.setSize(400, 400); this.add(new JLabel("Have seen the SplashScreen?")); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); displayCountDown(); this.setVisible(true); } private void displayCountDown() { SplashScreen ss = SplashScreen.getSplashScreen(); if(ss == null) return; Graphics2D g = ss.createGraphics(); Dimension d = ss.getSize(); for(int n=10; n>0; n--) { if(n == 5) { try{ ss.setImageURL(getClass().getResource("sun.gif"));} catch(Exception ex) {ex.printStackTrace();} ss.update(); d = ss.getSize(); } g.setColor(Color.green); g.fillRoundRect(0, d.height-20, d.width, 20, 5, 5); g.setColor(Color.red); g.setFont(new Font("Arial Narrow",Font.BOLD, 16)); String message = "Not a rocket launch: "+n; g.drawString(message,(d.width-g.getFontMetrics().stringWidth(message))/2, d.height-3); ss.update(); try { Thread.currentThread().sleep(1000L); } catch(Exception ex) {ex.printStackTrace();} } } public static void main(String arg[]) { new SplashScreenDisplay(); } } |

Fig. 1 Splash Screen for the First 5 seconds of the Count Down (image: ant.jpg)

Fig. 2 Splash Screen for the Last 5 seconds of the Count Down (image: sun.gif)
The splash images (ant.jpg and sun.gif) are available in the system\cdrom\desktop\src folder. Splash screen is displayed by calling java –splash:ant.jpg SplashScreenDisplay. This loads the ant image first. The displayCountDown() method changes the image after 5 seconds by calling setImageURL method of SplashScreen class.
There is an alternate way for displaying splash images without specifying the -splash option. For this, the images, and the class file are to be packed into a jar file. The manifest for the jar file should include the line SplashScreen-Image: ant.jpg in addition to the regular tags. The contents of manifest information file (named manifest.temp) are shown below:
Manifest-Version: 1.0
Main-Class: SplashScreenDisplay
SplashScreen-Image: ant.jpg
Issuing the command jar cfm SplashScreenDisplay.jar manifest.temp SplashScreenDisplay.class ant.jpg sun.gif creates the package. Issuing the command java -jar SplashScreenDisplay.jar can run the jar file. This command also produces splash display similar to the one previously seen.

Fig. 3 Window for which Splash Screen was displayed
Accessing Desktop Features
The second part of the article explains the procedure for using the features available in the native desktop environment for opening a file, editing a file, browsing, and printing a file just as if the operation is carried out from outside our program. These facilities are packed into java.awt.Desktop class. The methods called edit, open, print, browse, and mail are useful for launching files, and URIs in the appropriate program. The sample code opens a HTML file for editing in FrontPage, opens a word document in MS word, prints another word document, launches browser to display a small HTML file, and opens the default mailing program for sending email. The programs used for appropriate actions are determined from the system registry settings. Hence, the editor, or browser may differ from one system to the other, based on local software configurations. Hence, do not expect the same programs to come up on your system.
| ........................................................................................................................................................... import java.awt.*; public class DesktopDemo { public static void main(String arg[]) { if(!Desktop.isDesktopSupported()) { javax.swing.JOptionPane.showMessageDialog(null, "Desktop is not supported on this environment!"); return; } Desktop desk = Desktop.getDesktop(); try { desk.edit(new java.io.File("welcome.html"));} catch(Exception ex) {ex.printStackTrace();} try { desk.open(new java.io.File("welcome.doc"));} catch(Exception ex) {ex.printStackTrace();} try { desk.print(new java.io.File("welcome-print.doc"));} catch(Exception ex) {ex.printStackTrace(); } try { desk.browse(new java.io.File("welcome.html").toURI()); } catch(Exception ex) {ex.printStackTrace();} try { desk.mail();} catch(Exception ex) {ex.printStackTrace();} } } |



Cross Platform Performance
The desktop performance of both splash screen display program, and the desktop integration program was tested on Linux platform under KDE, and GNOME desktop environments. The performance of the splash screen program was just as it appeared on Windows. As the desktop integration program depends on availability of analogous features in Linux platform (and since I did not take care to configure my printer, and mailer on Linux), only two of the five desktop operations succeeded on my system. The word document (welcome.doc) was displayed on OpenOffice.org Writer, and the HTML file (welcome.html) was opened on Mozilla Firefox Browser. The editing of HTML file, printing of word document, and mailing failed on my system since I did not have the required software, or settings. But they will work on a system, which does have the right settings.

Handling Desktop
The new APIs for splash screen display, and desktop integration will help to improve the quality of Java programs, and reduce the burden of programmers in making the system dependent on guesswork to handle various file formats.
The author is V. Nagaradjane, Research Scholar, Annamalai University. You can email at nagaradjanev@rediffmail.com to know more.
