Java GUI - Web Browser and Opening Link
To open a url in the system's web browser you can use java.awt.Desktop.browse(URI). This allows you to keep your Java code platform independent, and even allows you to check to see if an operation is supported before trying to use it.
To load a web page within Java, I've had some success using the JavaFX WebView.
How do you open web pages in Java?
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.
Open a link in browser with java button?
Use the Desktop#browse(URI) method. It opens a URI in the user's default browser.
public static boolean openWebpage(URI uri) {
Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
try {
desktop.browse(uri);
return true;
} catch (Exception e) {
e.printStackTrace();
}
}
return false;
}
public static boolean openWebpage(URL url) {
try {
return openWebpage(url.toURI());
} catch (URISyntaxException e) {
e.printStackTrace();
}
return false;
}
How to put webpage into GUI windows in JAVA
You would need a web browser component for the rendering of your HTML.
See the thread here for a list of possible browser component.
You can see nice screenshots on the The DJ Project web page as well.
open a link in different browser java swing
Generally speaking, to open a link in the user's default browser you should really be using the more modern approach:
String url = "www.stackoverflow.com";
Desktop desktop = java.awt.Desktop.getDesktop();
desktop.browse(url);
No need to mess about with working out what OS you are running on (as your linked example attempts to do). Far better to let java.awt.Desktop
take care of finding an appropriate browser or application to open the URL (see the documentation for more details).
There's also a part of the API that gracefully handles permissions and 'unusual' OS setups. If there is the possibility that your code will run under a restricted security policy or on a platform that may not have a browser, then you can check up front rather than wait for the exception from the call to browse
.
// check if java.awt.Desktop is available on the current platform
java.awt.Desktop.isDesktopSupported();
// check the current platform and security policy will let you browse to a url
Desktop desktop = java.awt.Desktop.getDesktop();
desktop.isSupported(Desktop.Action.BROWSE);
Display a webpage inside a swing application
Use a JEditorPane
:
JEditorPane jep = new JEditorPane();
jep.setEditable(false);
try {
jep.setPage("http://www.yoursite.com");
}catch (IOException e) {
jep.setContentType("text/html");
jep.setText("<html>Could not load</html>");
}
JScrollPane scrollPane = new JScrollPane(jep);
JFrame f = new JFrame("Test HTML");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(scrollPane);
f.setPreferredSize(new Dimension(800,600));
f.setVisible(true);
Display webpage inside swing application
With plain Swing you're out of luck for that. However, if you'd consider switching your technology stack there might be alternatives:
Java FX WebView
Eclipse SWT can embed native Browser widget.
CSSBox
For others you might want to check out Pure Java HTML viewer/renderer for use in a Scrollable pane
Related Topics
How to Use an Arraylist as a Prepared Statement Parameter
Java.Sql.Sqlexception: Column Count Doesn't Match Value Count at Row 1
Exposed Beyond App Through Clipdata.Item.Geturi
Android Data Binding Using Include Tag
Decompile an APK, Modify It and Then Recompile It
Issue When Using a Custom Font - "Native Typeface Cannot Be Made"
Exporting and Running Unity3D Project to Android Studio
How to Access Downloads Folder in Android
Get Application Context from Non Activity Singleton Class
Meaning of the Import Statement in a Java File
How to Add Mouselistener to Item on Java Swing Canvas
Java Equivalent to PHP's Hmac-Sha1
Getextractedtext on Inactive Inputconnection Warning on Android
Android Resource - Array of Arrays
How to Execute Dex: Gc Overhead Limit Exceeded in Eclipse
How to Delete a File on Google Drive Using Google Drive Android API