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. |
Introduction to Computers, the Internet and the World Wide Web
Introduction to Java Applications
Introduction to Classes and Objects
Control Statements: Part I
Control Statements: Part 2
Methods: A Deeper Look
Arrays
Classes and Objects: A Deeper Look
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Polymorphism
GUI Components: Part 1
Graphics and Java 2D™
Exception Handling
Files and Streams
Recursion
Searching and Sorting
Data Structures
Generics
Collections
Introduction to Java Applets
Multimedia: Applets and Applications
GUI Components: Part 2
Multithreading
Networking
Accessing Databases with JDBC
Servlets
JavaServer Pages (JSP)
Formatted Output
Strings, Characters and Regular Expressions
Appendix A. Operator Precedence Chart
Appendix B. ASCII Character Set
Appendix C. Keywords and Reserved Words
Appendix D. Primitive Types
Appendix E. (On CD) Number Systems
Appendix F. (On CD) Unicode®
Appendix G. Using the Java API Documentation
Appendix H. (On CD) Creating Documentation with javadoc
Appendix I. (On CD) Bit Manipulation
Appendix J. (On CD) ATM Case Study Code
Appendix K. (On CD) Labeled break and continue Statements
Appendix L. (On CD) UML 2: Additional Diagram Types
Appendix M. (On CD) Design Patterns
Appendix N. Using the Debugger
Inside Back Cover