Source of WebBrowserPlus.java


  1: //WebBrowserPlus.java
  2: //Retrieves a file from a web server as HTML.

  4: import java.awt.Color;
  5: import java.awt.Dimension;
  6: import java.awt.FlowLayout;
  7: import java.awt.event.ActionEvent;
  8: import java.awt.event.ActionListener;
  9: import java.io.IOException;
 10: import java.io.BufferedReader;
 11: import java.io.InputStreamReader;
 12: import java.net.MalformedURLException;
 13: import java.net.URL;
 14: import javax.swing.JButton;
 15: import javax.swing.JEditorPane;
 16: import javax.swing.JFrame;
 17: import javax.swing.JScrollPane;
 18: import javax.swing.JTextField;

 20: public class WebBrowserPlus extends JFrame
 21: implements ActionListener
 22: {
 23:     private JTextField sourceURL;
 24:     private JEditorPane contents;
 25:     private JButton displayButton;

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

 35:     WebBrowserPlus(String s)
 36:     {
 37:         super(s);
 38:         setLayout(new FlowLayout());
 39:         sourceURL = new JTextField("http://", 50);
 40:         sourceURL.setCaretPosition(7);
 41:         displayButton = new JButton("Enter a URL in the box above "  +
 42:                                     "and click here to display the " +
 43:                                     "page at that location ...");
 44:         contents = new JEditorPane();
 45:         contents.setPreferredSize(new Dimension(540, 300));
 46:         contents.setEditable(false);
 47:         add(sourceURL);
 48:         add(displayButton);
 49:         JScrollPane scroller = new JScrollPane(contents);
 50:         add(scroller);
 51:         displayButton.addActionListener(this);
 52:         getContentPane().setBackground(Color.CYAN);
 53:     }
 54: 
 55:     public void actionPerformed(ActionEvent event)
 56:     {
 57:         String location = sourceURL.getText();
 58:         contents.setText("");
 59:         try
 60:         {
 61:             URL url = new URL(location);
 62:             contents.setPage(url);
 63:             contents.setCaretPosition(0);
 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: }