Java Reference
In-Depth Information
Displaying HTML
documents and
accessing the web
22
We now discuss how HTML-formatted text is displayed in Swing components. We
shall first show how files containing HTML text can be displayed. Then we show how
to make a simple browser which can access and display HTML-formatted web pages.
In Chapters 12 and 18 we saw how ASCII texts can be displayed and edited. An-
other commonly used text type is HTML ( HyperText Mark-up Language ). Prac-
tically all web pages are HTML-formatted and so are many software manuals that
are supplied on a CD. Files with HTML-formatted text are ASCII files that contain
the text to be displayed and additional formatting instructions. These instructions,
called HTML-tags , are enclosed in less-than and greater-than signs. For example,
the sequence
<center>HTML and Swing</center>
means that the text 'HTML and Swing' is displayed in a horizontally centred fash-
ion. When loading an HTML file into a Swing component for ASCII text, no format-
ting is done; the HTML-tags are shown as they appear in the text. The programs
that display HTML text in a formatted fashion have to interpret the HTML-tags,
and display the remaining text as required by these formatting instructions. Such
programs are, for example, web browsers.
We do not have to write an HTML interpreter ourselves to format HTML text.
The Swing library contains components with a built-in HTML interpreter, e.g. JEd-
itorPane . These components can display most HTML documents in a formatted
fashion. One has to be aware that the HTML specification has changed a number
of times, in particular new HTML-tags were added which allow more sophisticated
formatting. The HTML interpreter of Java knows the tags of a specific version of
HTML; newer or non-standardized formatting instructions are ignored. Thus the
Swing components might display some new, 'stylish' web pages somewhat differ-
ently from a standard web browser.
22.1
Displaying an HTML page
We present a program SimpleHTMLFrame which uses a JEditorPane to display
HTML text. The editor pane receives a URL ( uniform resource locator )inthe
Search WWH ::




Custom Search