GA Tech Mike
GA Tech Mike

Reputation: 163

How do you open web pages in Java?

Is there a simple way to open a web page within a GUI's JPanel?

If not, how do you open a web page with the computer's default web browser?

I am hoping for something that I can do with under 20 lines of code, and at most would need to create one class. No reason for 20 though, just hoping for little code...

I am planning to open a guide to go with a game. The guide is online and has multiple pages, but the pages link to each other, so I am hoping I only have to call one URL with my code.

Upvotes: 16

Views: 78669

Answers (6)

ElementCodez
ElementCodez

Reputation: 27

Please try below code :

import java.awt.Desktop;
import java.net.URI;
import java.net.URISyntaxExaception;

//below is the code
//whatvever the url is it has to have https://

Desktop d = Desktop.getDesktop();
try {
    d.browse(new URI("http://google.com"));
} catch (IOException | URISyntaxException e2) {
    e2.printStackTrace();
} 

Upvotes: -3

Joachim Sauer
Joachim Sauer

Reputation: 308249

Opening a web page with the default web browser is easy:

java.awt.Desktop.getDesktop().browse(theURI);

Embedding a browser is not so easy. JEditorPane has some HTML ability (if I remember my limited Swing-knowledge correctly), but it's very limited and not suiteable for a general-purpose browser.

Upvotes: 34

Michael Myers
Michael Myers

Reputation: 192035

There are two standard ways that I know of:

  1. The standard JEditorPane component
  2. Desktop.getDesktop().browse(URI) to open the user's default browser (Java 6 or later)

    Soon, there will also be a third:

  3. The JWebPane component, which apparently has not yet been released

JEditorPane is very bare-bones; it doesn't handle CSS or JavaScript, and you even have to handle hyperlinks yourself. But you can embed it into your application more seamlessly than launching FireFox would be.

Here's a sample of how to use hyperlinks (assuming your documents don't use frames):

// ... initialize myEditorPane
myEditorPane.setEditable(false); // to allow it to generate HyperlinkEvents
myEditorPane.addHyperlinkListener(new HyperlinkListener() {
    public void hyperlinkUpdate(HyperlinkEvent e) {
        if (e.getEventType() == HyperlinkEvent.EventType.ENTERED) {
            myEditorPane.setToolTipText(e.getDescription());
        } else if (e.getEventType() == HyperlinkEvent.EventType.EXITED) {
            myEditorPane.setToolTipText(null);
        } else if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
            try {
                myEditorPane.setPage(e.getURL());
            } catch (IOException ex) {
                // handle error
                ex.printStackTrace();
            }
        }
    }
});

Upvotes: 2

cobbal
cobbal

Reputation: 70785

haven't tried it at all, but the cobra HTML parser and viewer from the lobo browser, a browser writen in pure java, may be what you're after. They provide sample code to set up an online html viewer:

import javax.swing.*;
import org.lobobrowser.html.gui.*;
import org.lobobrowser.html.test.*;

public class BareMinimumTest {
  public static void main(String[] args) throws Exception {
    JFrame window = new JFrame();
    HtmlPanel panel = new HtmlPanel();
    window.getContentPane().add(panel);
    window.setSize(600, 400);
    window.setVisible(true);
    new SimpleHtmlRendererContext(panel, new SimpleUserAgentContext())
    .navigate("http://lobobrowser.org/browser/home.jsp");
  }
}

Upvotes: 0

Rich Apodaca
Rich Apodaca

Reputation: 29014

If you're developing an applet, you can use AppletContext.showDocument. That would be a one-liner:

getAppletContext().showDocument("http://example.com", "_blank");

If you're developing a desktop application, you might try Bare Bones Browser Launch.

Upvotes: 0

Geo
Geo

Reputation: 96957

I don't know if such a built-in exists, but I would use the Runtime class's exec with iexplore.exe or firefox.exe as an argument.

Upvotes: -1

Related Questions