16

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.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
GA Tech Mike
  • 163
  • 1
  • 1
  • 5
  • 1
    Possible duplicate of [Getting java gui to open a webpage in web browser](http://stackoverflow.com/questions/602032/getting-java-gui-to-open-a-webpage-in-web-browser) – Nathan Nov 09 '15 at 22:00
  • See also http://stackoverflow.com/questions/6360744/how-to-render-basic-html-markup-inside-a-jpanel-in-java-swing – David J. Liszewski Nov 10 '15 at 03:38

6 Answers6

34

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.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • Desktop API is only for java 1.6 – dfa Apr 14 '09 at 19:28
  • 9
    @dfa: so? There was no requirement for anything earlier and it's reasonable to use up-to-date APIs. – Joachim Sauer Apr 14 '09 at 19:59
  • 3
    Note that if your URI is a string, you need to wrap it with `new java.net.URI(the URI)` – Mikaël Mayer Sep 11 '15 at 08:18
  • This solution is not cross-platform, this thread is better answering this question: [How to open the default webbrowser using java](https://stackoverflow.com/questions/5226212/how-to-open-the-default-webbrowser-using-java) – Cleber Jorge Amaral Nov 17 '19 at 01:58
  • @CleberJorgeAmaral this is as cross platform as it gets. The top answer on the question you linked to uses the same class as my answer here. – Joachim Sauer Nov 18 '19 at 07:48
2

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();
            }
        }
    }
});
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Michael Myers
  • 188,989
  • 46
  • 291
  • 292
  • Just curious, but how could I have improved this answer? – Michael Myers Apr 14 '09 at 19:49
  • saua said "JEditorPane has some HTML ability, but it's very limited and not suiteable for a general-purpose browser." so i just went with the more simplistic one line of code saua posted... I was not going for something very nice looking, i am just trying to put together something that functions – GA Tech Mike Apr 14 '09 at 20:15
  • I will probably give your code a try later on, but i did not try to run it... so i did not want to click that it was an answer since i don't actually know – GA Tech Mike Apr 14 '09 at 20:18
  • Well, I also gave you saua's way before saua posted it. That's what the last sentence is referring to (did I make it unclear?). – Michael Myers Apr 14 '09 at 21:08
  • yea, i saw them both at the same time and he had the code in the box thing, so it stuck out more.. i did not realize you posted the same thing.. – GA Tech Mike Apr 14 '09 at 21:10
  • i tend to read the starts of something which i kinda expect to be a quick summary of the rest of the post... so i guess you could mention the different methods at the beginning and then explain each one after that – GA Tech Mike Apr 14 '09 at 21:12
  • Good point. (And thanks for answering the question in my first comment!) – Michael Myers Apr 14 '09 at 21:41
0

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.

Rich Apodaca
  • 28,316
  • 16
  • 103
  • 129
0

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");
  }
}
cobbal
  • 69,903
  • 20
  • 143
  • 156
-1

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.

Geo
  • 93,257
  • 117
  • 344
  • 520
-2

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();
} 
LuFFy
  • 8,799
  • 10
  • 41
  • 59