URLViewer

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( );
 south.add(loadButton);
 this.getContentPane( ).add(BorderLayout.SOUTH, south);
 theURL.addActionListener(this);
 loadButton.addActionListener(this);
 this.setLocation(50, 50);
 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 this.pack( );
 }
 public void actionPerformed(ActionEvent event) {
 try {
 URL u = new URL(theURL.getText( ));
 InputStream in = u.openStream( );
 OutputStream out = theDisplay.getOutputStream( );
 theDisplay.setText("");
 for (int c = in.read(); c != -1; c = in.read( )) {
 out.write(c);
 }
 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
 SwingUtilities.invokeLater(
 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

Buffers

Channels

Nonblocking I/O

The File System

Working with Files

File Dialogs and Choosers

Text

Character Sets and Unicode

Readers and Writers

Formatted I/O with java.text

Devices

The Java Communications API

USB

The J2ME Generic Connection Framework

Bluetooth

Character Sets



Java I/O
Java I/O
ISBN: 0596527500
EAN: 2147483647
Year: 2004
Pages: 244

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