O'Reilly logo

Java Examples in a Nutshell, 3rd Edition by David Flanagan

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

A Simple Web Browser

The two previous examples have shown us the powerful JTable and JTree components. A third powerful Swing component is javax.swing.text.JTextComponent and its various subclasses, which include JTextField, JTextArea, and JEditorPane. 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 JEditorPane to display static HTML text. In Java 1.2.2 and later, the JLabel, 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>".

Example 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, the 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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required