Source of ReadServerFile.java


  1: // Fig. 24.3: ReadServerFile.java
  2: // Use a JEditorPane to display the contents of a file on a Web server.
  3: import java.awt.BorderLayout;
  4: import java.awt.event.ActionEvent;
  5: import java.awt.event.ActionListener;
  6: import java.io.IOException;
  7: import javax.swing.JEditorPane;
  8: import javax.swing.JFrame;
  9: import javax.swing.JOptionPane;
 10: import javax.swing.JScrollPane;
 11: import javax.swing.JTextField;
 12: import javax.swing.event.HyperlinkEvent;
 13: import javax.swing.event.HyperlinkListener;
 14: 
 15: public class ReadServerFile extends JFrame 
 16: {
 17:    private JTextField enterField; // JTextField to enter site name
 18:    private JEditorPane contentsArea; // to display Web site
 19: 
 20:    // set up GUI
 21:    public ReadServerFile()
 22:    {
 23:       super( "Simple Web Browser" );
 24: 
 25:       // create enterField and register its listener
 26:       enterField = new JTextField( "Enter file URL here" );
 27:       enterField.addActionListener(
 28:          new ActionListener() 
 29:          {
 30:             // get document specified by user
 31:             public void actionPerformed( ActionEvent event )
 32:             {
 33:                getThePage( event.getActionCommand() );
 34:             } // end method actionPerformed
 35:          } // end inner class
 36:       ); // end call to addActionListener
 37: 
 38:       add( enterField, BorderLayout.NORTH );
 39: 
 40:       contentsArea = new JEditorPane(); // create contentsArea
 41:       contentsArea.setEditable( false );
 42:       contentsArea.addHyperlinkListener(
 43:          new HyperlinkListener() 
 44:          {
 45:             // if user clicked hyperlink, go to specified page
 46:             public void hyperlinkUpdate( HyperlinkEvent event )
 47:             {
 48:                if ( event.getEventType() == 
 49:                     HyperlinkEvent.EventType.ACTIVATED )
 50:                   getThePage( event.getURL().toString() );
 51:             } // end method hyperlinkUpdate
 52:          } // end inner class
 53:       ); // end call to addHyperlinkListener
 54: 
 55:       add( new JScrollPane( contentsArea ), BorderLayout.CENTER );
 56:       setSize( 400, 300 ); // set size of window
 57:       setVisible( true ); // show window
 58:    } // end ReadServerFile constructor
 59: 
 60:    // load document
 61:    private void getThePage( String location )
 62:    {
 63:       try // load document and display location 
 64:       {
 65:          contentsArea.setPage( location ); // set the page
 66:          enterField.setText( location ); // set the text
 67:       } // end try
 68:       catch ( IOException ioException ) 
 69:       {
 70:          JOptionPane.showMessageDialog( this,
 71:             "Error retrieving specified URL", "Bad URL", 
 72:             JOptionPane.ERROR_MESSAGE );
 73:       } // end catch
 74:    } // end method getThePage
 75: } // end class ReadServerFile
 76: 
 77: 
 78: /**************************************************************************
 79:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 80:  * Pearson Education, Inc. All Rights Reserved.                           *
 81:  *                                                                        *
 82:  * DISCLAIMER: The authors and publisher of this book have used their     *
 83:  * best efforts in preparing the book. These efforts include the          *
 84:  * development, research, and testing of the theories and programs        *
 85:  * to determine their effectiveness. The authors and publisher make       *
 86:  * no warranty of any kind, expressed or implied, with regard to these    *
 87:  * programs or to the documentation contained in these books. The authors *
 88:  * and publisher shall not be liable in any event for incidental or       *
 89:  * consequential damages in connection with, or arising out of, the       *
 90:  * furnishing, performance, or use of these programs.                     *
 91:  *************************************************************************/