Source of MiniBrowser.java


  1: // MiniBrowser.java
  2: // Retrieves a file from a web server as raw text.

  4: import java.awt.*;
  5: import java.awt.event.*;
  6: import java.net.*;
  7: import java.io.*;

  9: public class MiniBrowser
 10:     extends Frame
 11:     implements ActionListener
 12: {
 13:     private TextField txtInput;
 14:     private TextArea contents;
 15:     private Button btnDisplay;
 16:     private Button exit;

 18:     public static void main(String [] args)
 19:     {
 20:         MiniBrowser m = new MiniBrowser();
 21:         m.makeGUI();
 22:         m.setSize(550, 400);
 23:         m.setVisible(true);
 24:     }

 26:     public void makeGUI()
 27:     {
 28:         setLayout(new FlowLayout());

 30:         txtInput = new TextField(50);
 31:         add(txtInput);
 32:         btnDisplay = new Button("Display page at this URL");
 33:         add(btnDisplay);

 35:         exit = new Button("Exit");
 36:         add(exit);
 37:         exit.addActionListener(this);

 39:         contents =
 40:             new TextArea("", 20, 60, TextArea.SCROLLBARS_VERTICAL_ONLY);
 41:         add(contents);
 42:         btnDisplay.addActionListener(this);
 43:     }

 45:     public void actionPerformed(ActionEvent event)
 46:     {
 47:         if (event.getSource() == exit)
 48:             System.exit(0);

 50:         String line;
 51:         String location = txtInput.getText();

 53:         try
 54:         {
 55:             URL url = new URL(location);
 56:             BufferedReader input = new
 57:                 BufferedReader(new InputStreamReader(url.openStream()));
 58:             while ((line = input.readLine()) != null)
 59:             {
 60:                  contents.append(line);
 61:                  contents.append("/n");
 62:             }
 63:             input.close();
 64:         }
 65:         catch (MalformedURLException e)
 66:         {
 67:             contents.setText("Invalid URL Format");
 68:         }
 69:         catch (IOException io)
 70:         {
 71:             contents.setText(io.toString());
 72:         }
 73:     }
 74: }