devxlogo

HTML browser in Java

HTML browser in Java

Question:
I am tring to make a stand-alone application that can read HTML files and render them like a Web browser. How can I do this in pure Java?

Answer:
The Swing classes provide the ability to parse and render HTML 3.2 files. The underlying support for this functionality is provided by the com.sun.java.swing.text and com.sun.java.swing.html packages, but you don’t have to become overly familiar with those packages to get started displaying HTML files. JEditorPane, in com.sun.java.swing, can display text, rich text, and HTML. All you have to do is pass the URL of the file you want to display to the JEditorPane constructor. If you want only to display the file, and not edit it, you must remember to invoke setEditable with an argument of false. Unfortunately, JEditorPane does not by default support the traversal hyperlinks. To implement browser-like hyperlink traversal, you must create a HyperlinkListener and write the necessary code to load new URL’s. The following example demonstrates the basic steps that are necessary to do this. The program accepts a URL as an argument an displays the file. For example,java DisplayHTML file:/usr/local/jdk1.1.6/docs/api/packages.html could be used to display the Java API documentation on your local hard drive.

import java.awt.*;import java.awt.event.*;import java.io.*;import java.net.*;import com.sun.java.swing.*;import com.sun.java.swing.event.*;import com.sun.java.swing.text.*;public final class DisplayHTML {  public static void main(String[] args) {    final JEditorPane htmlPane;    JScrollPane scrollPane;    JFrame frame;    WindowListener exitListener;    HyperlinkListener linkListener;    URL url;    if(args.length != 1) {      System.err.println("Usage: DisplayHTML url");      return;    }    try {      url = new URL(args[0]);    } catch(MalformedURLException e) {      e.printStackTrace();      return;    }    try {      htmlPane = new JEditorPane(url);    } catch(IOException e) {      e.printStackTrace();      return;    }    // We only want to display the file, not edit it.    htmlPane.setEditable(false);    linkListener = new HyperlinkListener() {      public void hyperlinkUpdate(HyperlinkEvent e) {	URL newURL;	Document currentDocument;	if(e.getEventType() != HyperlinkEvent.EventType.ACTIVATED)	  return;	if((newURL = e.getURL()) == null)	  return;	currentDocument = htmlPane.getDocument();	try {	  htmlPane.setPage(newURL);	} catch(IOException ex) {	  htmlPane.setDocument(currentDocument);	}      }    };    htmlPane.addHyperlinkListener(linkListener);    scrollPane = new JScrollPane();    scrollPane.getViewport().add(htmlPane);    frame = new JFrame("HTML Demo: " + args[0]);    frame.getContentPane().add(scrollPane);    exitListener = new WindowAdapter() {      public void windowClosing(WindowEvent e) {	Window window = e.getWindow();	window.setVisible(false);	window.dispose();	System.exit(0);      }    };    frame.addWindowListener(exitListener);    frame.pack();    frame.setVisible(true);  }}
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist