Reading a File on a Web Server

Our next example once again hides the networking details from us. The application in Fig. 24.3 uses Swing GUI component JEditorPane (from package javax.swing) to display the contents of a file on a Web server. The user inputs a URL in the JTextField at the top of the window, and the application displays the corresponding document (if it exists) in the JEditorPane. Class JEditorPane is able to render both plain text and HTML-formatted text, as illustrated in the two screen captures (Fig. 24.4), so this application acts as a simple Web browser. The application also demonstrates how to process HyperlinkEvents when the user clicks a hyperlink in the HTML document. The techniques shown in this example can also be used in applets. However, an applet is allowed to read files only on the server from which it was downloaded.

Figure 24.3. Reading a file by opening a connection through a URL.

(This item is displayed on pages 1114 - 1115 in the print version)

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

Figure 24.4. Test class for ReadServerFile.

(This item is displayed on pages 1115 - 1116 in the print version)

1 // Fig. 24.4: ReadServerFileTest.java 2 // Create and start a ReadServerFile. 3 import javax.swing.JFrame; 4 5 public class ReadServerFileTest 6 { 7 public static void main( String args[] ) 8 { 9 ReadServerFile application = new ReadServerFile(); 10 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); 11 } // end main 12 } // end class ReadServerFileTest  

The application class ReadServerFile contains JTextField enterField, in which the user enters the URL of the file to read and JEditorPane contentsArea to display the contents of the file. When the user presses the Enter key in enterField, the application calls method actionPerformed (lines 3134). Line 33 uses ActionEvent method getActionCommand to get the string the user input in the JTextField and passes the string to utility method getThePage (lines 6174).

Line 65 invokes JEditorPane method setPage to download the document specified by location and display it in the JEditorPane. If there is an error downloading the document, method setPage throws an IOException. Also, if an invalid URL is specified, a MalformedURLException (a subclass of IOException) occurs. If the document loads successfully, line 66 displays the current location in enterField.

Typically, an HTML document contains hyperlinkstext, images or GUI components which, when clicked, provide quick access to another document on the Web. If a JEditorPane contains an HTML document and the user clicks a hyperlink, the JEditorPane generates a HyperlinkEvent (package javax.swing.event) and notifies all registered HyperlinkListeners (package javax.swing.event) of that event. Lines 4253 register a HyperlinkListener to handle HyperlinkEvents. When a HyperlinkEvent occurs, the program calls method hyperlinkUpdate (lines 4651). Lines 4849 use HyperlinkEvent method getEventType to determine the type of the HyperlinkEvent. Class HyperlinkEvent contains a public nested class called EventType that declares three static EventType objects, which represent the hyperlink event types. ACTIVATED indicates that the user clicked a hyperlink to change Web pages, ENTERED indicates that the user moved the mouse over a hyperlink and EXITED indicates that the user moved the mouse away from a hyperlink. If a hyperlink was ACTIVATED, line 50 uses HyperlinkEvent method getURL to obtain the URL represented by the hyperlink. Method toString converts the returned URL to a string that can be passed to utility method getThePage.

Look-and-Feel Observation 24.1

A JEditorPane generates HyperlinkEvents only if it is uneditable.

Категории