Swing provides a GUI component named javax.swing.JEditorPane that can display plain text, HTML, and RTF files automatically. Using it you don't have to write code to explicitly read data from the files. JEditorPane is a subclass of JTextComponent . Thus it inherits all the behavior and properties of JTextComponent .
To display the content of a file, use the setPage(URL) method, as follows :
public void setPage(URL url) throws IOException
JEditorPane generates javax.swing.event.HyperlinkEvent when a hyperlink in the editor pane is clicked. Through this event, you can get the URL of the hyperlink and display it using the setPage(url) method.
Listing 25.11 gives an example that creates a simple Web browser to render HTML files. The program lets the user enter an HTML file in a text field and press the Enter key to display it in an editor pane, as shown in Figure 25.14.
1 import java.awt.*; 2 import java.awt.event.*; 3 import javax.swing.*; 4 import java.net.URL; 5 import javax.swing.event.*; 6 import java.io.*; 7 8 public class WebBrowser extends JApplet { 9 // JEditor pane to view HTML files 10 private JEditorPane jep = new JEditorPane(); 11 12 // Label for URL 13 private JLabel jlblURL = new JLabel( "URL" ); 14 15 // Text field for entering URL 16 private JTextField jtfURL = new JTextField(); 17 18 /** Initialize the applet */ 19 public void init() { 20 // Create a panel jpURL to hold the label and text field 21 JPanel jpURL = new JPanel(); 22 jpURL.setLayout( new BorderLayout()); 23 jpURL.add(jlblURL, BorderLayout.WEST); 24 jpURL.add(jtfURL, BorderLayout.CENTER); 25 26 // Create a scroll pane to hold JEditorPane 27 JScrollPane jspViewer = new JScrollPane(); 28 jspViewer.getViewport().add(jep, null ); 29 30 // Place jpURL and jspViewer in the applet 31 add( jspViewer , BorderLayout.CENTER); 32 add(jpURL, BorderLayout.NORTH); 33 34 // Set jep noneditable 35 jep.setEditable( false ); 36 37 // Register listener 38 jep.addHyperlinkListener( new HyperlinkListener() { 39 public void hyperlinkUpdate(HyperlinkEvent e) { 40 try { 41 jep.setPage(e.getURL()); 42 } 43 catch (IOException ex) { 44 System.out.println(ex); 45 } 46 } 47 }); 48 jtfURL.addActionListener( new ActionListener() { 49 public void actionPerformed(ActionEvent e) { 50 try { 51 // Get the URL from text field 52 URL url = new URL(jtfURL.getText().trim()); 53 54 // Display the HTML file 55 jep.setPage(url); 56 } 57 catch (IOException ex) { 58 System.out.println(ex); 59 } 60 } 61 }); 62 } 63 } |
In this example, a simple Web browser is created using the JEditorPane class (line 10). JEditorPane is capable of displaying files in HTML format. To enable scrolling, the editor pane is placed inside a scroll pane (lines 27 “28).
The user enters the URL of the HTML file in the text field and presses the Enter key to fire an action event to display the URL in the editor pane. To display the URL in the editor pane, simply set the URL in the page property of the editor pane (line 55).
The editor pane does not have all the functions of a commercial Web browser, but it is convenient for displaying HTML files, including embedded images.
There are two shortcomings in this program: (1) it cannot view a local HTML file, and (2) to view a remote HTML file, you have to enter a URL beginning with http:// . In Exercise 25.9, you will modify the program so that it can also view an HTML file from the local host and accept URLs beginning with either http:// or www .