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);  }}
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS