Example 5-5 is the URLViewer program foreshadowed in Chapter 2. URLViewer 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.) The application has a text field in which the user can type a URL, a Load button that the user presses to load the specified URL, and a JStreamedTextArea component from Chapter 2 that displays the text from the URL. Each of these corresponds to a field in the URLViewer class.

Example 5-5. The URLViewer program

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import java.net.*;
import com.elharo.io.ui.*;
public class URLViewer extends JFrame
 implements ActionListener {
 private JTextField theURL = new JTextField( );
 private JButton loadButton = new JButton("Load");
 private JStreamedTextArea theDisplay = new JStreamedTextArea(60, 72);
 public URLViewer( ) {
 super("URL Viewer");
 this.getContentPane( ).add(BorderLayout.NORTH, theURL);
 JScrollPane pane = new JScrollPane(theDisplay);
 this.getContentPane( ).add(BorderLayout.CENTER, pane);
 Panel south = new Panel( );
 this.getContentPane( ).add(BorderLayout.SOUTH, south);
 this.setLocation(50, 50);
 this.pack( );
 public void actionPerformed(ActionEvent event) {
 try {
 URL u = new URL(theURL.getText( ));
 InputStream in = u.openStream( );
 OutputStream out = theDisplay.getOutputStream( );
 for (int c = in.read(); c != -1; c = in.read( )) {
 in.close( );
 catch (IOException ex) {
 theDisplay.setText("Invalid URL: " + ex.getMessage( ));
 public static void main(String args[]) {
 final URLViewer me = new URLViewer( );
 // To avoid deadlock don't show frames on the main thread
 new Runnable( ) {
 public void run( ) {
 me.show( );

The URLViewer class itself extends JFrame. The constructor builds the interface, which consists of a JTextField to type the URL into, a JStreamedTextArea component from Chapter 2 that is placed inside a JScrollPane, and a Load button that can be pressed to download the content of the URL.

The streamed text area is filled when the user clicks the Load button or presses Enter inside the URL text field. The URLViewer object listens to both of these components. The URLViewer's actionPerformed( ) method constructs a URL from the text in the text field, then opens an input stream from the URL in the text field. Data from the URL's input stream pours into the text area's output stream. When that's finished, the input stream is closed. The output stream, however, is left open so the user can view new URLs.

Basic I/O

Introducing I/O

Output Streams

Input Streams

Data Sources

File Streams

Network Streams

Filter Streams

Filter Streams

Print Streams

Data Streams

Streams in Memory

Compressing Streams

JAR Archives

Cryptographic Streams

Object Serialization

New I/O



Nonblocking I/O

The File System

Working with Files

File Dialogs and Choosers


Character Sets and Unicode

Readers and Writers

Formatted I/O with java.text


The Java Communications API


The J2ME Generic Connection Framework


Character Sets

show all menu

Java I/O
Java I/O
ISBN: 0596527500
EAN: 2147483647
Year: 2004
Pages: 244
Similar book on Amazon

Flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net