Create a web browser in java
There are several libraries in Java that allow you to display a web page, but most of them were discontinued a few years ago. In this article, we will see how to create a browser and display the content of a website.
You can display a web page in three ways:
You can display a web page in three ways:
- JEditorPane: included in the JRE, but displays only simple HTML pages.
- JWebBrowser: integrates the Internet Explorer browser.
- JxBrowser: integrates the Chromium browser, it is the most efficient way because it allows the display of modern web pages designed with HTML5/CSS3, JavaScript, Siverlight... The only downside is that it is paid.
The JEditorPane class
The JEditorPane allows you to display only HTML content, JEditorPane is useful when you want to open and control the display of HTML files.
The following example shows a simple HTML web page without CSS.
The resulting display is static and you can't do anything except view. We want to make hyperlinks clickable by associating a listener with hyperlinks:
The development of JWebBrowser is based on the following frameworks and a implementation of SWT that provides rich components. JWebBrowser offers a rich browser containing a Flash player, a media player and an HTML editor.
Here is the link for download the JWebBrowser library. You can also download and test the application online. ici.
The use of JWebBrowser is very simple as shown in the following code:
The JxBrowser API is paid and very expensive, it is recommended only if you really need it and in large companies. A one-month trial version is offered, just enter your name and email address at the bottom of the page to get the license key.
Here is the link to JxBrowser download from their official website. You can also run the demo and test the application.
The following code shows the use of the JxBrowser.
References:
How to Use Editor Panes and Text Panes
JWebBrowser Class (DJ Native Swing - SWT) - The DJ project
Class JWebBrowser
import javax.swing.text.html.HTMLEditorKit;Output:
import javax.swing.*;
import java.io.*;
import java.net.URL;
import java.awt.*;
public class JEditorTest {
public static void main(String[] args) {
JEditorPane jep = new JEditorPane();
jep.setEditable(false);
jep.setBackground(Color.WHITE);
jep.setBounds(316, 110, 598, 545);
HTMLEditorKit kit = new HTMLEditorKit();
jep.setEditorKit(kit);
try {
jep.setPage(new URL("http://imss-www.upmf-grenoble.fr/prevert/Prog/Java/swing/JEditorPane.html"));
}
catch (IOException e) {
jep.setContentType("text/html ");
jep.setText("unable to view the page");
}
JScrollPane scrollPane = new JScrollPane(jep);
JFrame f = new JFrame();
// Next line requires Java 1.3
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(scrollPane);
f.setSize(512, 342);
f.show();
}
}
The resulting display is static and you can't do anything except view. We want to make hyperlinks clickable by associating a listener with hyperlinks:
jep.addHyperlinkListener(new HyperlinkListener() {JEditorPane also allows you to get the information or edit a web page.
public void hyperlinkUpdate(HyperlinkEvent e) {
if(e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
JEditorPane jepane = (JEditorPane)e.getSource();
try {
jepane.setPage(e.getURL());
}catch (IOException ex) {
jepane.setText("ERROR: "+ex.getMessage());
}
}
}
});
JWebBrowser
JWebBrowser is a library that allows easy integration into the components of the Swing application to create a web browser that imports Internet Explorer or Mozilla on Windows and Mozilla on other operating systems. You need to have the latest version of Internet Explorer installed to avoid component lag and CSS stylesheets.The development of JWebBrowser is based on the following frameworks and a implementation of SWT that provides rich components. JWebBrowser offers a rich browser containing a Flash player, a media player and an HTML editor.
Here is the link for download the JWebBrowser library. You can also download and test the application online. ici.
The use of JWebBrowser is very simple as shown in the following code:
import java.awt.BorderLayout;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import chrriis.common.UIUtils;
import chrriis.dj.nativeswing.swtimpl.NativeInterface;
import chrriis.dj.nativeswing.swtimpl.components.JWebBrowser;
public class JWebBrowser {
public static JComponent createContent() {
JPanel contentPane = new JPanel(new BorderLayout());
JWebBrowser webBrowser = new JWebBrowser();
webBrowser.navigate("http://www.orange.fr");
contentPane.add(webBrowser);
return contentPane;
}
public static void main(String[] args) {
NativeInterface.open();
UIUtils.setPreferredLookAndFeel();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(createContent(),
BorderLayout.CENTER);
frame.setSize(800, 600);
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
});
NativeInterface.runEventPump();
}
}
JxBrowser
JxBrowser is the most complete API because it integrates the browser Chromium which is an extension of Chrome for linux. JxBrowser is made with Swing and JavaFX, which makes the display of web pages designed in HTML5/CSS3, JavaScript, Siverlight... excellent and without any problems.The JxBrowser API is paid and very expensive, it is recommended only if you really need it and in large companies. A one-month trial version is offered, just enter your name and email address at the bottom of the page to get the license key.
Here is the link to JxBrowser download from their official website. You can also run the demo and test the application.
The following code shows the use of the JxBrowser.
import java.awt.BorderLayout;
import javax.swing.JFrame;
import com.teamdev.jxbrowser.chromium.Browser;
import com.teamdev.jxbrowser.chromium.swing.BrowserView;
public class JxBrowserTest {
public static void main(String[] args) {
Browser browser = new Browser();
BrowserView view = new BrowserView(browser);
JFrame frame = new JFrame();
frame.add(view, BorderLayout.CENTER);
frame.setSize(700, 500);
frame.setVisible(true);
browser.loadURL("http://www.youtube.com");
}
}
References:
How to Use Editor Panes and Text Panes
JWebBrowser Class (DJ Native Swing - SWT) - The DJ project
Class JWebBrowser