• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Launch default web browser from swing

 
Anna Hays
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think codes for bringing up a default browser is system dependent and should be avoided. I might be wrong though.
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Daniel Dalton
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic