The two previous examples have shown us the powerful
JTree components. A third powerful Swing
javax.swing.text.JTextComponent and its
various subclasses, which include
JEditorPane is a particularly interesting
component that makes it easy to display (or edit) HTML text.
As an aside, it is worth noting here that you do not
have to create a
display static HTML text. In Java 1.2.2 and later, the
JButton, and other similar components can
all display multiline, multifont formatted HTML labels. The trick is
to begin the label with the string "<html>". This tells the
component to treat the rest of the label string as formatted HTML text
and display it (using an internal
JTextComponent) in that way. You can
experiment with the feature using the
ShowBean program; use it to create a
JButton component and set the
text property to a value that
begins with "<html>".
11-21 is a listing of WebBrowser.java, a
JFrame subclass that implements the
simple web browser shown in Figure 11-18. The
WebBrowser class uses the power of the
java.net.URL class to download HTML
documents from the Web and the
JEditorPane component to display the
contents of those documents. Although defined as a reusable component,
WebBrowser class includes a
main( ) method so that it can be
run as a standalone application.
Figure 11-18. The WebBrowser component
Example 11-21 is intended as a demonstration ...