Skip to content
June 19, 2009 / triasmara

Swing Text Components and HTML Rendering

Many applications present text to the user for viewing and editing. This text may consist of
plain, unformatted characters, or it may consist of richly styled characters that use multiple
fonts and extensive formatting. Swing provides three basic types of text components for
presenting and editing text. Class JTextComponent is the base class for all Swing text
components, including JTextField, JTextArea and JEditorPane.
JTextField is a single-line text component suitable for obtaining simple user input
or displaying information such as form field values, calculation results and so on. JPass-
wordField is a subclass of JTextField suitable for obtaining user passwords. These
components do not perform any special text styling. Rather, they present all text in a single
font and color. JTextArea, like JTextField and JPasswordField, also does not
style its text.

However, JTextArea does provide a larger visible area and supports larger
plain-text documents.
JEditorPane provides enhanced text-rendering capabilities. JEditorPane sup-
ports styled documents that include formatting, font and color information. JEditor-
Pane is capable of rendering HTML documents as well as Rich Text Format (RTF)
documents. We use class JEditorPane to render HTML pages for a simple Web-
browser application. JTextPane is a JEditorPane subclass that renders only styled
documents, and not plain text. JTextPane provides developers with fine-grained control
over the style of each character and paragraph in the rendered document.
WebBrowserPane (Fig. 2.1) extends class JEditorPane to create a Web-
browsing component that maintains a history of visited URLs. Line 16 creates a List for
keeping track of visited URLs. Line 23 invokes method setEditable of class JEdi-
torPane to disable text editing in the WebBrowserPane. JEditorPane enables
hyperlinks in HTML documents only if the JEditorPane is not editable.

1 //
2 // WebBrowserPane is a simple Web-browsing component that
3 // extends JEditorPane and maintains a history of visited URLs.
4 package com.deitel.advjhtp1.gui.webbrowser;
6 // Java core packages
7 import java.util.*;
8 import*;
9 import*;
11 // Java extension packages
12 import javax.swing.*;
14 public class WebBrowserPane extends JEditorPane {
16 private List history = new ArrayList();
17 private int historyIndex;
19 // WebBrowserPane constructor
20 public WebBrowserPane()
21 {
22 // disable editing to enable hyperlinks
23 setEditable( false );
24 }
26 // display given URL and add it to history
27 public void goToURL( URL url )
28 {
29 displayPage( url );
30 history.add( url );
31 historyIndex = history.size() – 1;
32 }
34 // display next history URL in editorPane
35 public URL forward()
36 {
37 historyIndex++;
39 // do not go past end of history
40 if ( historyIndex >= history.size() )
41 historyIndex = history.size() – 1;
43 URL url = ( URL ) history.get( historyIndex );
44 displayPage( url );
46 return url;
47 }
49 // display previous history URL in editorPane
50 public URL back()
51 {
52 historyIndex–;
54 // do not go past beginning of history
55 if ( historyIndex < 0 )
56 historyIndex = 0;
58 // display previous URL
59 URL url = ( URL ) history.get( historyIndex );
60 displayPage( url );
62 return url;
63 }
65 // display given URL in JEditorPane
66 private void displayPage( URL pageURL )
67 {
68 // display URL
69 try {
70 setPage( pageURL );
71 }
73 // handle exception reading from URL
74 catch ( IOException ioException ) {
75 ioException.printStackTrace();
76 }
77 }
78 }
Method goToURL (lines 27–32) navigates the WebBrowserPane to the given URL.
Line 29 invokes method displayPage of class WebBrowserPane to display the given
URL. Line 30 invokes method add of interface List to add the URL to the browser his-
tory. Line 31 updates the historyIndex to ensure that methods back and forward
navigate to the appropriate URL.
Method forward (lines 35–47) navigates the WebBrowserPane to the next page
in the URL history. Line 37 increments historyIndex, and lines 43–44 retrieve the
URL from the historyList and display the URL in WebBrowserPane. If the his-
toryIndex is past the last page in the history, line 41 sets historyIndex to the
last URL in history.
Method back (lines 50–63) navigates WebBrowserPane to the previous page in the
URL history. Line 52 decrements historyIndex, and lines 55–56 ensure that histo-
ryIndex does not fall below 0. Lines 59–60 retrieve the URL and display it in the Web-
Method displayPage takes as an argument a URL to display in the WebBrowser-
Pane. Line 70 invokes method setPage of class JEditorPane to display the page that
the URL references. Lines 74–76 catch an IOException if there is an error loading the
page from the given URL.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: