Using the Desktop API in Java SE 6
With the default graphical user interface (GUI) look and feel, printing, and performance, the Java platform has come a long way in its effort to minimize the difference between the performance and integration of native applications and Java applications. Version 6 of the Java Platform, Standard Edition (Java SE), continues to narrow the gap with new system tray functionality, better print support for
This new functionality is provided by the
The Desktop API uses your host operating system's file associations to launch applications associated with specific file types. For example, if OpenDocument text (
Running the DesktopDemo Application
DesktopDemo is a simple Java application that uses the new Desktop API. The application has one main window that exposes the entire API, allowing you to do three things:
Figure 1 shows the user interface (UI).
You can run this application by downloading the application source and
Determining Desktop API Support
Before launching the browser, email client, or any application, DesktopDemo must determine whether your platform supports the API. First, however, DesktopDemo disables all the graphical text fields and buttons. It enables these graphical components after determining that the platform supports them.
After instantiating the UI, the application's constructor quickly disables the few components of this application as shown in the following code:
If your application doesn't check for API support using
Once retrieved, the
Before invoking any of these actions, an application should determine whether the
Using a new
Once the application determines the supported actions, it enables the appropriate graphical components. If all components are enabled, the UI should look like Figure 3.
Opening the Browser
Calling the following instance method will open your host's default browser:
Because DesktopDemo's UI components are enabled only if the associated
DesktopDemo adds a
If all goes well, however, the listener retrieves the text from the associated text field in Figure 4, creates a URI, and invokes the
Applications can launch the host's default email client, if that action is supported, by calling this
DesktopDemo has an
You can use more than just a single email recipient when creating this URI. The
Figure 7 shows the result.
You can, of course, invoke
Opening, Editing, and Printing a File
Java applications can open, edit, and print files from their associated application using a
Each of DesktopDemo's radio buttons has its own
When you press the Launch Default Application button, it invokes its own listener, which calls the following method:
This method determines which
Interestingly, different applications may be registered for these different actions even on the same file type. For example, the Firefox browser may be launched for the
Desktop integration is an important theme. One way that Java SE 6 technology supports this theme is through the
Note: Any API additions or other enhancements to the Java SE platform specification are subject to review and approval by theJSR 270 Expert Group.
For More Information
The following information may help you learn more about Java SE 6's integration features as well as related topics: