JaVa
   

The JEditorPane

A really neat component is the JEditorPane, as you can easily load and display HTML pages in it. This can have many uses, as it is a really easy way to display lots of preformatted information to the user. Let's look at a simple example that uses the JEditorPane and loads in a sample web page called index.html, which is stored in the same directory as the source.

Code Listing 18: Using the JEditorPane to load HTML
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
public class JEditorPaneExample extends JFrame implements
 HyperlinkListener
{
 public static void main(String[] argv)
 {
 JEditorPaneExample mainApp = new JEditorPaneExample();
 }
 public JEditorPaneExample()
 {
 super("JEditorPaneExample Example");
 setBounds(0, 0, 600, 400);
 getContentPane().setLayout(null);
 setDefaultCloseOperation(EXIT_ON_CLOSE);
 // Attempt to load an 'index' html page...
 try
 {
 URL url = null;
 try
 {
 url = getClass().getResource("index.html");
 }
 catch(Exception e)
 {
 System.out.println("Could not open file!");
 url = null;
 }
 if(url != null)
 {
 htmlViewer = new JEditorPane(url);
 htmlViewer.setEditable(false);
 htmlViewer.addHyperlinkListener(this);
 scrollpane = new JScrollPane();
 scrollpane.setBounds(10, 10, 570, 350);
 scrollpane.getViewport().add(htmlViewer);
 }
 }
 catch(MalformedURLException e)
 {
 System.out.println(e);
 }
 catch(IOException e)
 {
 System.out.println(e);
 }
 getContentPane().add(scrollpane);
 setVisible(true);
 }
 public void hyperlinkUpdate(HyperlinkEvent e)
 {
 if(e.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
 {
 if(e instanceof HTMLFrameHyperlinkEvent)
 {
 ((HTMLDocument) htmlViewer.getDocument()).processHTML
 FrameHyperlinkEvent((HTMLFrameHyperlinkEvent) e);
 }
 else
 {
 try
 {
 htmlViewer.setPage(e.getURL());
 }
 catch(IOException e2)
 {
 System.out.println(e2);
 }
 }
 }
 }
 JEditorPane htmlViewer;
 JScrollPane scrollpane;
}


Java End example

When this code is executed, it will attempt to load the index.html page from the same directory as the code and should look like Screenshot on the following page. Note that the sample index.html file contains both an internal and external link. Clicking on the Wordware logo will actually load up the real Wordware web site into the Java app if you have an Internet connection available, whereas clicking the link that is displayed below the logo will simply load a page called page2.html from the same directory as the code.

NoteĀ 

The index.html and page2.html files, as well as the logo, can also be found on this CD-ROM.

Java Click To expand
Screenshot: The JEditorPane example app

Let's now take a look at how the app works. First we attempt to load our HTML page (in this case, index.html) into our app using the following line of code:

url = getClass().getResource("index.html");


If the file can be read successfully, we then create a JEditorPane object, passing the URL object into the constructor. This can be seen in the following line of code:

htmlViewer = new JEditorPane(url);


Next we need to set two properties of the JEditorPane to make it suit our purpose, which is to display an HTML page. We first set it so that the user cannot edit the content by calling the setEditable method as follows:

htmlViewer.setEditable(false);


Then we add a HyperlinkListener, which is assigned to this instance of the main class that you may have noticed extends a HyperlinkListener. This is achieved with the following line of code:

htmlViewer.addHyperlinkListener(this);


Do not worry about the HyperlinkListener; the explanation will follow soon. Next we create a JScrollPane object and add our JEditorPane object to its viewport. This is to allow the HTML page to scroll if there is too much information on it to fit in the editor pane all at once. This is accomplished using the following three lines of code:

scrollpane = new JScrollPane();
scrollpane.setBounds(10, 10, 570, 350);
scrollpane.getViewport().add(htmlViewer);


Finally our scrollpane object is added to the content pane with the following line of code:

getContentPane().add(scrollpane);


Let's now look at our implementation of the HyperlinkListener. The HyperlinkListener interface defines one method called hyperlinkUpdate, which we must implement. This function is called whenever a hyperlink is interacted with within the HTML document. First we check if a hyperlink has been activated with the following if statement:

if(e.getEventType() == HyperlinkEvent.EventType.ACTIVATED)


If so, we then attempt to load in the HTML page using the following block of code. Note that the following if statement checks if the link is a local reference inside the page, and if so it does not unnecessarily update the page.

 if(e instanceof HTMLFrameHyperlinkEvent)
 {
 ((HTMLDocument) htmlViewer.getDocument()).processHTML
 FrameHyperlinkEvent((HTMLFrameHyperlinkEvent) e);
 }
 else
 {
 try
 {
 htmlViewer.setPage(e.getURL());
 }
 catch(IOException e2)
 {
 System.out.println(e2);
 } }


NoteĀ 

Note that it's not a great browser for surfing the web, but when you know what content you are going to display (meaning, you can test it), it works great (such as for displaying help).

JaVa
   
Comments