The Desktop class is a part of Java AWT package. This class is used to launch associated applications registered on the native desktop to handle a URI or a file.
Important Points About Desktop Class :
- It can open a default web browser showing a specific URI
- It can launch default mail client with optional mailto URI
- It can launch a registered application to open, edit or print a specific file.
Different Methods in Desktop Class
| Method | Explanation |
|---|---|
| browse(URI u) | Launches the default browser to display a specific URI. |
| edit(File f) | Launches the associated editor application and opens a file. |
| getDesktop() | Returns the Desktop instance of the current browser context. |
| isDesktopSupported() | returns whether this class is supported on the current platform. |
| isSupported(Desktop.Action action) | returns whether an action is supported on the current platform. |
| mail() | Launches the mail composing window of the user default mail client. |
| mail(URI mailtoURI) | Launches the mail composing window of the user default mail client, filling the message fields specified by a mailto: URI. |
| open(File f) | Launches the associated application to open the file |
| print(File f) | Prints a file with the native desktop printing facility, using the associated application’s print command. |
Below programs illustrate the Desktop class in Java AWT:
- Program to Launch the browser and open a specific URI
// Java Program to Launch the browser// and open a specific URIimportjava.awt.*;importjavax.swing.*;importjava.awt.event.*;importjava.net.*;ÂÂclassdeskextendsJFrameimplementsActionListener {   Â// frame   ÂstaticJFrame f;    Â   Â// Main Method   Âpublicstaticvoidmain(String args[])   Â{       Âdesk d =newdesk();       Â// create a frame       Âf =newJFrame("desktop");       Â// create a panel       ÂJPanel p =newJPanel();       Â// create a button       ÂJButton b =newJButton("launch");       Â// add Action Listener       Âb.addActionListener(d);       Âp.add(b);       Âf.add(p);       Âf.show();       Âf.setSize(200,200);   Â}   Â// if button is pressed   ÂpublicvoidactionPerformed(ActionEvent e)   Â{       Âtry{           Â// create a URI           ÂURI u =newURI("www.geeksforgeeks.org");           ÂDesktop d = Desktop.getDesktop();           Âd.browse(u);       Â}       Âcatch(Exception evt) {       Â}   Â}}Output :
- Program to Launch the mail to a specific URI
// Java Program to Launch the// mail to a specific URIimportjava.awt.*;importjavax.swing.*;importjava.awt.event.*;importjava.net.*;ÂÂclassdeskextendsJFrameimplementsActionListener {   Â// frame   ÂstaticJFrame f;   Â// Main Method   Âpublicstaticvoidmain(String args[])   Â{       Âdesk d =newdesk();       Â// create a frame       Âf =newJFrame("desktop");       Â// create a panel       ÂJPanel p =newJPanel();       Â// create a button       ÂJButton b =newJButton("launch");       Â// add Action Listener       Âb.addActionListener(d);       Âp.add(b);       Âf.add(p);       Âf.show();       Âf.setSize(200,200);   Â}   Â// if button is pressed   ÂpublicvoidactionPerformed(ActionEvent e)   Â{       Âtry{           ÂURI u =newURI("mailto:contribute@geeksforgeeks.org");           ÂDesktop d = Desktop.getDesktop();           Âd.mail(u);       Â}       Âcatch(Exception evt) {           ÂJOptionPane.showMessageDialog(this, evt.getMessage());       Â}   Â}}Output:
- Program to open a file
// Java Program to open a fileimportjava.awt.*;importjavax.swing.*;importjava.awt.event.*;importjava.io.*;importjava.net.*;classdeskextendsJFrameimplementsActionListener {   Â// frame   ÂstaticJFrame f;     Â   Â// Main Method   Âpublicstaticvoidmain(String args[])   Â{       Âdesk d =newdesk();       Â// create a frame       Âf =newJFrame("desktop");       Â// create a panel       ÂJPanel p =newJPanel();       Â// create a button       ÂJButton b =newJButton("launch");       Â// add Action Listener       Âb.addActionListener(d);       Âp.add(b);       Âf.add(p);       Âf.show();       Âf.setSize(200,200);   Â}   Â// if button is pressed   ÂpublicvoidactionPerformed(ActionEvent e)   Â{       Âtry{           Â// create a file           ÂFile u =newFile("f:\\image.png");           ÂDesktop d = Desktop.getDesktop();           Âd.open(u);       Â}       Âcatch(Exception evt) {           ÂJOptionPane.showMessageDialog(this, evt.getMessage());       Â}   Â}}Output :
- Program to open a file for editing
// Java Program to open a file for editingimportjava.awt.*;importjavax.swing.*;importjava.awt.event.*;importjava.io.*;importjava.net.*;ÂÂclassdeskextendsJFrameimplementsActionListener {   Â// frame   ÂstaticJFrame f;   Â// Main Method   Âpublicstaticvoidmain(String args[])   Â{       Âdesk d =newdesk();       Â// create a frame       Âf =newJFrame("desktop");       Â// create a panel       ÂJPanel p =newJPanel();       Â// create a button       ÂJButton b =newJButton("launch");       Â// add Action Listener       Âb.addActionListener(d);       Âp.add(b);       Âf.add(p);       Âf.show();       Âf.setSize(200,200);   Â}   Â// if button is pressed   ÂpublicvoidactionPerformed(ActionEvent e)   Â{       Âtry{           Â// create a file           ÂFile u =newFile("f:\\sample.txt");           ÂDesktop d = Desktop.getDesktop();           Âd.edit(u);       Â}       Âcatch(Exception evt) {           ÂJOptionPane.showMessageDialog(this, evt.getMessage());       Â}   Â}}Output :
- Program to open a file for printing
// Java Program to open a file for printingimportjava.awt.*;importjavax.swing.*;importjava.awt.event.*;importjava.io.*;importjava.net.*;ÂÂclassdeskextendsJFrameimplementsActionListener {   Â// frame   ÂstaticJFrame f;   Â// Main Method   Âpublicstaticvoidmain(String args[])   Â{       Âdesk d =newdesk();       Â// create a frame       Âf =newJFrame("desktop");       Â// create a panel       ÂJPanel p =newJPanel();       Â// create a button       ÂJButton b =newJButton("launch");       Â// add Action Listener       Âb.addActionListener(d);       Âp.add(b);       Âf.add(p);       Âf.show();       Âf.setSize(200,200);   Â}   Â// if button is pressed   ÂpublicvoidactionPerformed(ActionEvent e)   Â{       Âtry{           Â// create a file           ÂFile u =newFile("f:\\sample.txt");           ÂDesktop d = Desktop.getDesktop();           Âd.print(u);       Â}       Âcatch(Exception evt) {           ÂJOptionPane.showMessageDialog(this, evt.getMessage());       Â}   Â}}Output :
Reference: https://docs.oracle.com/javase/7/docs/api/java/awt/Desktop.html

