Source of WebBrowser.java


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

  4: import java.awt.Color;
  5: import java.awt.FlowLayout;
  6: import java.awt.event.ActionEvent;
  7: import java.awt.event.ActionListener;
  8: import java.io.BufferedReader;
  9: import java.io.InputStreamReader;
 10: import java.io.IOException;
 11: import java.net.MalformedURLException;
 12: import java.net.URL;
 13: //Note:
 14: //Scanner causes exception when used in place of BufferedReader
 15: import java.util.Scanner;
 16: import javax.swing.JButton;
 17: import javax.swing.JFrame;
 18: import javax.swing.JScrollPane;
 19: import javax.swing.JTextField;
 20: import javax.swing.JTextArea;

 22: public class WebBrowser extends JFrame
 23: implements ActionListener
 24: {
 25:     private JTextField sourceURL;
 26:     private JTextArea contents;
 27:     private JButton displayButton;

 29:     public static void main(String [] args)
 30:     {
 31:         WebBrowser app = new WebBrowser("Mini Web Browser");
 32:         app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 33:         app.setSize(580, 390);
 34:         app.setVisible(true);
 35:     }

 37:     WebBrowser(String s)
 38:     {
 39:         super(s);
 40:         setLayout(new FlowLayout());
 41:         getContentPane().setBackground(Color.CYAN);

 43:         sourceURL = new JTextField("http://", 50);
 44:         sourceURL.setCaretPosition(7);
 45:         add(sourceURL);

 47:         displayButton = new JButton("Enter a URL in the box above "  +
 48:                                     "and click here to display the " +
 49:                                     "page at that location ...");
 50:         displayButton.addActionListener(this);
 51:         add(displayButton);

 53:         contents = new JTextArea(20, 50);
 54:         contents.setEditable(false);
 55:         JScrollPane scroller = new JScrollPane(contents);
 56:         add(scroller);
 57:     }
 58: 
 59:     public void actionPerformed(ActionEvent event)
 60:     {
 61:         String location = sourceURL.getText();
 62:         contents.setText("");
 63:         try
 64:         {
 65:             String line;
 66:             URL url = new URL(location);
 67:             /*Why don't the following two lines of code work?
 68:              * That is, why do you need the next three lines of code instead?
 69:             Scanner input = new Scanner(url.openStream());
 70:             while ((line = input.nextLine()) != null)
 71:             */
 72:             InputStreamReader in = new InputStreamReader(url.openStream());
 73:             BufferedReader input = new BufferedReader(in);
 74:             while ((line = input.readLine()) != null)
 75:             {
 76:                  contents.append(line);
 77:                  contents.append("\n");
 78:             }
 79:             input.close();
 80:             contents.setCaretPosition(0);
 81:         }
 82:         catch (MalformedURLException e)
 83:         {
 84:             contents.setText("Invalid URL Format");
 85:         }
 86:         catch (IOException ioEx)
 87:         {
 88:             contents.setText(ioEx.toString());
 89:         }
 90:         catch (Exception e)
 91:         {
 92:             contents.setText("Unexpected exception: " + e.toString());
 93:         }
 94:     }
 95: }