Me Myself and My way

Posts tagged ‘desktop api’

Using the Desktop API in Java SE 6

Author : John O’Conner,

Source : http://java.sun.com/developer/technicalArticles/J2SE/Desktop/javase6/desktop_api/

Conclude by : ME

More info : http://java.sun.com/javase/6/docs/api/java/awt/Desktop.html

Desktop Overview

This new functionality is provided by the java.awt.Desktop class. The API is adopted from the JDesktop Integration Components (JDIC) project. The goal of that project is to make “Java technology-based applications first-class citizens” of the desktop, enabling seamless integration. Specifically, the new Desktop API allows your Java applications to do the following:

  • Launch the host system’s default browser with a specific Uniform Resource Identifier (URI)
  • Launch the host system’s default email client
  • Launch applications to open, edit, or print files associated with those applications

The Desktop API uses your host operating system’s file associations to launch applications associated with specific file types. For example, if OpenDocument text (.odt) file extensions are associated with the OpenOffice Writer application, your Java application could launch OpenOffice Writer to open, edit, or even print files with that association. Depending on your host system, different applications may be associated with each different action.

Opening the Browser

Calling the following instance method will open your host’s default browser:

    
    public void browse(URI uri) throws IOException

ex.

    
    private void onLaunchBrowser(java.awt.event.ActionEvent evt) {
        URI uri = null;
        try {
            uri = new URI(txtBrowserURI.getText());
            desktop.browse(uri);
        }
        catch(IOException ioe) {
            ioe.printStackTrace();
        }
        catch(URISyntaxException use) {
            use.printStackTrace();

        }
        ...
    }
Sending Email

Applications can launch the host’s default email client, if that action is supported, by calling this Desktop instance method:

    public void mail(URI uri) throws IOException

ex.

     private void onLaunchMail(java.awt.event.ActionEvent evt) {
        String mailTo = txtMailTo.getText();
        URI uriMailTo = null;
        try {
            if (mailTo.length() > 0) {
                uriMailTo = new URI("mailto", mailTo, null);
                desktop.mail(uriMailTo);
            } else {
                desktop.mail();
            }
        }
        catch(IOException ioe) {
            ioe.printStackTrace();
        }
        catch(URISyntaxException use) {
            use.printStackTrace();
        }
        ...
    }
Opening, Editing, and Printing a File

Java applications can open, edit, and print files from their associated application using a Desktop object’s open(), edit(), and print() methods, respectively (see Figure 8). Again, DesktopDemo allows these actions only if the Desktop instance supports them, so in this application scenario, it is not necessary to check for support again.

    private void onLaunchDefaultApplication(java.awt.event.ActionEvent evt) {
        String fileName = txtFile.getText();
        File file = new File(fileName);

        try {
            switch(action) {
                case OPEN:
                    desktop.open(file);
                    break;
                case EDIT:
                    desktop.edit(file);
                    break;
                case PRINT:
                    desktop.print(file);
                    break;
            }
        }
        catch (IOException ioe) {
            ioe.printStackTrace();
        }
        ...
    }