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 so
There are two standard ways that I know of:
Desktop.getDesktop().browse(URI) to open the user's default browser (Java 6 or later)
Soon, there will also be a third:
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();
}
}
}
});