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"));

35 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
  6. Science Channel’s Are Giving A Complete Knowledge To Its Viewers About Every Thing Students Write Done Dissertation On This Subjects And Show Its Importance.

    ReplyDelete
  7. This Is Really Great Work. Thank You For Sharing Such A Good And Useful Information Here In The Blog For Students.

    ReplyDelete
  8. Hi! This is kind of off topic but I need some advice from an established post. Is it hard to set up your own post? I'm not very techincal but I can figure things out pretty quick. I'm thinking about creating my own but I'm not sure where to start. Do you have any tips or suggestions? Appreciate it seo firm singapore

    ReplyDelete
  9. This article has suggested to me many new ideas. I will embark on doing it. Hope you can continue to contribute your talents in this area. Thank you.

    ReplyDelete
  10. I and my guys were actually examining the great procedures on your web blog and so suddenly came up with an awful feeling I had not expressed respect to the site owner for them. Most of the guys were definitely as a consequence thrilled to read all of them and have now absolutely been loving these things. Thanks for being indeed accommodating and also for choosing this form of remarkable tips most people are really eager to discover. Our own sincere apologies for not saying thanks to you sooner.digital marketing services

    ReplyDelete
  11. Astounding post about the English things. A few people basically leave all kind of affects and go for the outing. I feel cheerful that visit page you go for the outing and I am certain your mind before long feels better.

    ReplyDelete
  12. I am typically to running a blog and i really respect your content. The article has actually peaks my interest. I'm going to bookmark your web site and keep checking for brand spanking new information.golf courses western sydney

    ReplyDelete
  13. Really informative article.Really looking forward to read more. Really Cool.
    โปรโมชั่นGclub ของทางทีมงานตอนนี้แจกฟรีโบนัส 50%
    เพียงแค่คุณสมัคร Gclub กับทางทีมงานของเราเพียงเท่านั้น
    ร่วมมาเป็นส่วนหนึ่งกับเว็บไซต์คาสิโนออนไลน์ของเราได้เลยค่ะ
    สมัครสล็อตออนไลน์ >>> goldenslot
    สนใจร่วมลงทุนกับเรา สมัครเอเย่น Gclub คลิ๊กได้เลย

    ReplyDelete
  14. Im grateful for the blog post.Really thank you!
    เว็บไซต์คาสิโนออนไลน์ที่ได้คุณภาพอับดับ 1 ของประเทศ
    เป็นเว็บไซต์การพนันออนไลน์ที่มีคนมา Gclub Royal1688
    และยังมีหวยให้คุณได้เล่น สมัครหวยออนไลน์ ได้เลย
    สมัครสล็อตออนไลน์ได้ที่นี่ >>> Golden slot
    ร่วมลงทุนสมัครเอเย่น Gclubกับทีมงานของเราได้เลย

    ReplyDelete
  15. Write more, thats all I have to say. Literally, it seems as though you relied on the video to make your point. You clearly know what youre talking about, why throw away your intelligence on just posting videos to your blog when you could be giving us something enlightening to read? dental implant singapore

    ReplyDelete
  16. Fantastic blog! Do you have any recommendations for aspiring writers? I'm planning to start my own site soon but I'm a little lost on everything. Would you advise starting with a free platform like Wordpress or go for a paid option? There are so many options out there that I'm totally confused .. Any ideas? Thank you!
    golf clubs sydney

    ReplyDelete
  17. Hello! Someone in my Facebook group shared this site with us so I came to take a look. I'm definitely enjoying the information. I'm book-marking and will be tweeting this to my followers! Superb blog and brilliant design. divorce procedure singapore

    ReplyDelete
  18. Woah! I'm really loving the template/theme of this blog. It's simple, yet effective. A lot of times it's very difficult to get that "perfect balance" between superb usability and visual appeal. I must say you have done a very good job with this. Additionally, the blog loads super fast for me on Chrome. Exceptional Blog! Interior Designers Singapore

    ReplyDelete
  19. This is my 1st comment here so I just wanted to give a quick shout out and say I truly enjoy reading through your articles. Can you suggest any other blogs/websites/forums that cover the same topics? Thank you! M19A1

    ReplyDelete
  20. Have you ever thought about adding a little bit more than just your articles? I mean, what you say is valuable and everything. But just imagine if you added some great visuals or video clips to give your posts more, "pop"! Your content is excellent but with pics and video clips, this website could undeniably be one of the most beneficial in its niche. Excellent blog
    buy mattress singapore

    ReplyDelete
  21. A ton of much obliged for the entirety of your persistent effort on this site. Betty truly cherishes doing web exploration and it is evident why. My life partner and I become familiar with about the dynamic medium you present great recommendations on your web blog and too welcome reaction from others on the article while my little girl is truly being instructed to such an extent. Exploit the remainder of the year. You are doing a truly incredible work.

    cleaning service company

    ReplyDelete
  22. My significant other and I were very tranquil when Albert figured out how to wrap up his fundamental exploration through the valuable proposals he was given out of the site pages. It is sometimes baffling just to consistently be parting with directions which different people have been bringing in cash from. We truly recollect that we need you to express gratefulness to for this. The delineations you have made, the clear blog route, the companionships your webpage help induce - it has generally incredible, and it's truly driving our child notwithstanding us feel that the present circumstance is cool, and that is phenomenally significant. A debt of gratitude is in order for the entire thing!

    energy supplements

    ReplyDelete
  23. Well it seems like your site ate my first remark (it was too long) so I surmise I'll simply summarize it what I composed and state, I'm altogether making the most of your blog. I also am a hopeful blog author however I'm still new to the entire thing. Do you have any tips for amateur blog essayists? I'd like it

    safe deposit box

    ReplyDelete
  24. Do you mind if I quote a couple of your articles as long as I provide credit and sources back to your site? My website is in the exact same area of interest as yours and my visitors would definitely benefit from a lot of the information you present here. Please let me know if this ok with you. Thank you!bedding singapore

    ReplyDelete
  25. Thanks on your marvelous posting! I truly enjoyed reading it, you happen to be a great author.I will be sure to bookmark your blog and will come back later on. I want to encourage one to continue your great work, have a nice weekend!korean cosmetics

    ReplyDelete
  26. I’m no longer sure the place you're getting your information, however good topic. I must spend some time learning much more or figuring out more. Thank you for great info I used to be searching for this information for my mission.influencer marketing singapore

    ReplyDelete
  27. We are a group of volunteers and opening a new scheme in our community. Your site offered us with valuable information to work on. You've done a formidable job and our entire community will be grateful to you.personalised journal

    ReplyDelete
  28. hi!,I like your writing so much! share we communicate more about your post on AOL? I require an expert on this area to solve my problem. May be that's you! Looking forward to see you.private debt consolidation lenders

    ReplyDelete