Java AWT | Desktop Class
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 | Explanantion |
|---|---|
| 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 {// framestaticJFrame f;// Main Methodpublicstaticvoidmain(String args[]){desk d =newdesk();// create a framef =newJFrame("desktop");// create a panelJPanel p =newJPanel();// create a buttonJButton b =newJButton("launch");// add Action Listenerb.addActionListener(d);p.add(b);f.add(p);f.show();f.setSize(200,200);}// if button is pressedpublicvoidactionPerformed(ActionEvent e){try{// create a URIURI u =newURI("www.geeksforgeeks.org");Desktop d = Desktop.getDesktop();d.browse(u);}catch(Exception evt) {}}}chevron_rightfilter_noneOutput :
- 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 {// framestaticJFrame f;// Main Methodpublicstaticvoidmain(String args[]){desk d =newdesk();// create a framef =newJFrame("desktop");// create a panelJPanel p =newJPanel();// create a buttonJButton b =newJButton("launch");// add Action Listenerb.addActionListener(d);p.add(b);f.add(p);f.show();f.setSize(200,200);}// if button is pressedpublicvoidactionPerformed(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());}}}chevron_rightfilter_noneOutput:
- Program to open a file
// Java Program to open a fileimportjava.awt.*;importjavax.swing.*;importjava.awt.event.*;importjava.io.*;importjava.net.*;classdeskextendsJFrameimplementsActionListener {// framestaticJFrame f;// Main Methodpublicstaticvoidmain(String args[]){desk d =newdesk();// create a framef =newJFrame("desktop");// create a panelJPanel p =newJPanel();// create a buttonJButton b =newJButton("launch");// add Action Listenerb.addActionListener(d);p.add(b);f.add(p);f.show();f.setSize(200,200);}// if button is pressedpublicvoidactionPerformed(ActionEvent e){try{// create a fileFile u =newFile("f:\\image.png");Desktop d = Desktop.getDesktop();d.open(u);}catch(Exception evt) {JOptionPane.showMessageDialog(this, evt.getMessage());}}}chevron_rightfilter_noneOutput :

- 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 {// framestaticJFrame f;// Main Methodpublicstaticvoidmain(String args[]){desk d =newdesk();// create a framef =newJFrame("desktop");// create a panelJPanel p =newJPanel();// create a buttonJButton b =newJButton("launch");// add Action Listenerb.addActionListener(d);p.add(b);f.add(p);f.show();f.setSize(200,200);}// if button is pressedpublicvoidactionPerformed(ActionEvent e){try{// create a fileFile u =newFile("f:\\sample.txt");Desktop d = Desktop.getDesktop();d.edit(u);}catch(Exception evt) {JOptionPane.showMessageDialog(this, evt.getMessage());}}}chevron_rightfilter_noneOutput :
- 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 {// framestaticJFrame f;// Main Methodpublicstaticvoidmain(String args[]){desk d =newdesk();// create a framef =newJFrame("desktop");// create a panelJPanel p =newJPanel();// create a buttonJButton b =newJButton("launch");// add Action Listenerb.addActionListener(d);p.add(b);f.add(p);f.show();f.setSize(200,200);}// if button is pressedpublicvoidactionPerformed(ActionEvent e){try{// create a fileFile u =newFile("f:\\sample.txt");Desktop d = Desktop.getDesktop();d.print(u);}catch(Exception evt) {JOptionPane.showMessageDialog(this, evt.getMessage());}}}chevron_rightfilter_noneOutput :
Reference: https://docs.oracle.com/javase/7/docs/api/java/awt/Desktop.html
Recommended Posts:
- Implement Sextet Class from Quintet Class in Java using JavaTuples
- Implement Septet Class from Sextet Class in Java using JavaTuples
- Implement Quintet Class with Quartet Class in Java using JavaTuples
- Implement Decade Class from Ennead Class in Java using JavaTuples
- Implement Pair Class with Unit Class in Java using JavaTuples
- Implement Octet Class from Septet Class in Java using JavaTuples
- Implement Ennead Class from Octet Class in Java using JavaTuples
- Implement Triplet Class with Pair Class in Java using JavaTuples
- Implement Quartet Class with Triplet Class in Java using JavaTuples
- Difference between Abstract Class and Concrete Class in Java
- Using predefined class name as Class or Variable name in Java
- Java.lang.Class class in Java | Set 2
- Java.lang.Class class in Java | Set 1
- Java.util.concurrent.Phaser class in Java with Examples
- Java.util.concurrent.RecursiveAction class in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



