This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Launch default web browser from swing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Launch default web browser from swing" Watch "Launch default web browser from swing" New topic
Author

Launch default web browser from swing

Anna Hays
Ranch Hand

Joined: Nov 09, 2003
Posts: 131
Hi,

Is it possible to launch the default browser to display a URL from a Swing application? Not an applet. Can anyone shed some lights how to do it please? I can't find anything in Google that is not about Applets. I want to pop up a browser window for the user guide in the JMenuBar.

Thanks again!
Ed Tse
Ranch Hand

Joined: Sep 18, 2003
Posts: 183
I think codes for bringing up a default browser is system dependent and should be avoided. I might be wrong though.


SCJP, Pre-SCJD (URLyBird 1.3.1), Teradata Cert'd Prof
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42031
    
  64
I'm not sure what this has to do with SCJD (which this forum is about), but Eric Albert's BrowserLauncher class does a very good cross-platform job of starting a browser.


Ping & DNS - my free Android networking tools app
Daniel Dalton
Ranch Hand

Joined: Mar 20, 2005
Posts: 146
Hi Anna -

have a look at javax.swing.JTextPane - it's capable of displaying a simple html file, and can be embedded in a JScrollPane in a JPanel. Unless you're trying to do something really complicated, that would probably suffice.
Anna Hays
Ranch Hand

Joined: Nov 09, 2003
Posts: 131
The BrowserLauncher class looks too complicated for so little use. I want to pop up the browser for the user guide in the menu bar. Using TextPane looks complicated too. Maybe I should take away this option...
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42031
    
  64
The BrowserLauncher class is indeed complicated, but using it is extremely simple:

BrowserLauncher.openURL("file://...")

is all that's required. You can also pass in an HTTP URL.
Daniel Dalton
Ranch Hand

Joined: Mar 20, 2005
Posts: 146
A Textpane really isn't that complicated. It can load a file from a url via JTextPane.setPage(java.net.URL url) and the system classloader can give you a URL back to your help file. As for setting it up in a panel, it's just the same as any other swing GUI component. My help system class weighed in at 120 lines including lots of comments.

That said, you have to be happy with your own solution - best of luck!
Anna Hays
Ranch Hand

Joined: Nov 09, 2003
Posts: 131
BrowserLauncher seem to be a class that I want to rip off for work, it looks really handy.

Thanks for the help, I will look into TextPane more because I can't use the BrowserLauncher as part of my submission.
 
GeeCON Prague 2014
 
subject: Launch default web browser from swing