Saturday, November 16, 2013

Open browser with Desktop class

Desktop is a useful class in Java. It can be used to open the default browser for a specified url, open a default mail client with optional e-mail address, open a file for editing or viewing, and send a file to the default printer.
To do such operatons mentioned above, first you need to create an object of the Desktop class by using the getDesktop method.

Desktop dt=Desktop.getDesktop();

To launch the default browser on your computer with a specified url, you will use the browse method. The browse method has one argument that is a uri object referencing to the url. To obtain the uri object, you have to create a url object that accepts the address of a web page. Then use the toURI method to convert the url object to uri object. The example code below will open the default browser and shows the http://javatheprogram.blogspot.com address on its address box.


open browser


import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URL;

public class DesktopDemo {

public static void main(String args[]){
//openMail();
try {

URL url=new URL("http://javatheprogram.blogspot.com");
openBrowser(url.toURI());

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

//openFile("d:/chart.pdf");

}


public static void openBrowser(URI uri){
Desktop dt=Desktop.getDesktop();
if(Desktop.isDesktopSupported()){
try {

dt.browse(uri);

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

You can launch an e-mail client program with your specified e-mail address by using the mail method. The mail method accepts one value that is the uri object. When you construct an url object to refer to an e-mail address, the string that will be passed to the URL constructor takes this form: "mailto:e-mail_addresss". The example below will open the e-mail client program with the e-mail address yuk.dara@gmail.com on you machine.

public static void openMail(){
try {
URL url=new URL("mailto:yuk.dara@gmail.com");
Desktop dt=Desktop.getDesktop();
if(Desktop.isDesktopSupported()){
dt.mail(url.toURI());
}

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

To open a file on your local computer, you will use the open method. This method will take the file object that references to a file on your computer. The code below opens the test.txt file (in drive D) of the computer.

dt.open(new File("d://test.txt"));

If you wan to print the file to the default printer that connects to your computer, you can use the print method. This method also accepts the file object that you want to print. The following code will print the file chart.pdf to the default printer.

dt.print(new File("d:/chart.pdf"));

7 comments:

  1. Please Upload an file that related to the post.and put the download link below the post... :)

    ReplyDelete
  2. I know but sometimes i cannot combine it to a complete file, because i am poor about java :(

    ReplyDelete
  3. Its simply superb.Feeling good to share the link to practice java interview questions @ http://skillgun.com

    ReplyDelete
  4. This type of lesson on Javacript will make the students to become an expert Java developer.There are many lessons available in the online market place but the appropriate way of learning the different procedure of making Javascript like as http://www.procustomwriting.com/custom-research-papers is so different from the traditional way.Many people are looking to learn the Desktop class regarding the Javascript.

    ReplyDelete
  5. Click the link https://www.thewritingkid.com/academic-writing/ to save your time and manage everything. It is the best service for writing work for students and scientists. They can write about everything what your need and following your instructions to the end. Great team!

    ReplyDelete