How to open a URL from a Swing Component?

B

Bengan

Hi,

I have a Frame and want to be able to open an URL(HTML page) with a button
click. I mean like you do with the showDocument method under AppletContext.
But i cant find any method that does that.

Best Regards
Bengan
 
S

Sandip Chitale

Do you want to

1. use the platform browser to show the document or

2. use a Java panel to open the URL?

For 1, you will have to launch the browser with
Runtime.getRuntime().exec(). You can launch the external browser
program with URL as the argument. Some platforms allow you to simply
run the URL and the OS will launch the default browser (e.g. MS
Windows - Runtime.getRuntime().exec("http://www.google.com");

REF:
http://java.sun.com/j2se/1.3/docs/api/java/lang/Runtime.html#exec(java.lang.String)

For 2, you can use javax.swing.JEditorPane, add it to your frame,set
the content type to "text/html" and use
setPage("http://www.google.com");

REF:
http://java.sun.com/j2se/1.3/docs/api/javax/swing/JEditorPane.html
http://java.sun.com/docs/books/tutorial/uiswing/components/simpletext.html#editorpane

HTH,
sandip
 
R

Roedy Green

I have a Frame and want to be able to open an URL(HTML page) with a button
click. I mean like you do with the showDocument method under AppletContext.
But i cant find any method that does that.

You could do an HTTP GET, retrieve the message, then feed it to a
output = new JEditorPane();
output.setContentType("text/html");
output.setEditable(false);

Any pray Swing is smart enough to render your html.

See http://mindprod.com/jgloss/htmlrendering.html for your other
options.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top