FileDocCategorySizeDatePackage
URLViewer.javaAPI DocExample1611Fri Feb 03 16:31:52 GMT 2006None

URLViewer

public class URLViewer extends JFrame implements ActionListener

Fields Summary
private JTextField
theURL
private JButton
loadButton
private JStreamedTextArea
theDisplay
Constructors Summary
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();
  
Methods Summary
public voidactionPerformed(java.awt.event.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 voidmain(java.lang.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();
        }
      }
    );