O'Reilly logo

Java I/O by Elliotte Rusty Harold

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

URLViewer

Example 5.6 is an improved version of the URLViewer you first encountered in Chapter 2. This is a simple application that provides a window in which you can view the contents of a URL. It assumes that those contents are more or less ASCII text. (In future chapters, I’ll remove that restriction.) Figure 5.1 shows the result. Our application has a text area in which the user can type a URL, a Load button that the user uses to load the specified URL, and a StreamedTextArea component that displays the text from the URL. Each of these corresponds to a field in the URLViewer class.

The URLViewer

Figure 5-1. The URLViewer

Example 5-6. The URLViewer Program

import java.awt.*; import java.awt.event.*; import java.io.*; import java.net.*; import com.macfaq.awt.*; import com.macfaq.io.*; public class URLViewer extends Frame implements WindowListener, ActionListener { TextField theURL = new TextField(); Button loadButton = new Button("Load"); StreamedTextArea theDisplay = new StreamedTextArea(); public URLViewer() { super("URL Viewer"); } public void init() { this.add("North", theURL); this.add("Center", theDisplay); Panel south = new Panel(); south.add(loadButton); this.add("South", south); theURL.addActionListener(this); loadButton.addActionListener(this); this.addWindowListener(this); this.setLocation(50, 50); this.pack(); this.show(); } public void actionPerformed(ActionEvent evt) { try { URL ...

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