Chapter 23. Editor Panes and Editor KitsOver the last four chapters we've covered just about all the classes and interfaces that make up the Swing text framework. In this chapter, we'll look at a class that ties everything together: EditorKit . An EditorKit pulls together the document model, document view, document editing actions, and document I/O strategy, serving as a central reference point for a given document type.
In addition to looking at
EditorKit
and its subclasses, this chapter introduces the
TextAction
class (an abstract extension of
AbstractAction
) and the many useful concrete action classes available as inner classes of the
EditorKit
subclasses. These actions include basic functions such as copying and pasting text as well as style-oriented
Throughout the chapter, we build simple but powerful editors for working with increasingly complex content types, moving from plain text to styled text to HTML. Finally, we discuss the process for creating your own editor kit. |
23.1 The JEditorPane Class
JEditorPane
is an extension of
JTextComponent
capable of displaying various types of content, such as HTML and RTF. It is not intended to be used as a
JEditorPane
s work closely with
EditorKit
objects. An
EditorKit
plugs into the editor pane to customize it for a particular content type. Without an
EditorKit
telling it how to work, a
JEditorPane
can't function. We discuss
EditorKit
in the
Figure 23-1 shows the JEditorPane in action, displaying a portion of the Javadoc for the JEditorPane class. Here's the code: Figure 23-1. JEditorPane showing an HTML page
// HTMLExample.java
//
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
public class HTMLExample {
public static void main(String[] args) {
JEditorPane pane = null;
try {
pane = new JEditorPane(args[0]);
}
catch (IOException ex) {
ex.printStackTrace(System.err);
System.exit(1);
}
pane.setEditable(false);
// Add a hyperlink listener.
final JEditorPane finalPane = pane;
pane.addHyperlinkListener(new HyperlinkListener( ) {
public void hyperlinkUpdate(HyperlinkEvent ev) {
try {
if (ev.getEventType( ) == HyperlinkEvent.EventType.ACTIVATED)
finalPane.setPage(ev.getURL( ));
} catch (IOException ex) { ex.printStackTrace(System.err); }
}
});
JFrame frame = new JFrame( );
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new JScrollPane(pane));
frame.setSize(350,400);
frame.setVisible(true);
}
}
We've created a minimal HTML browser.
[1]
In a real application, you'd want to do things like change the cursor while new pages are being loaded and handle exceptions more elegantly. The anonymous inner class in this example shows a quick way to enable
23.1.1 PropertiesTable 23-1 shows the properties defined by JEditorPane . The accessibleContext property depends on the type of EditorKit in use. If an HTMLEditorKit is installed, a special AccessibleJEditorPaneHTML object is used. Otherwise, its superclass, AccessibleJEditorPane , is used. AccessibleJEditorPane extends the JTextComponent.AccessibleJTextComponent class. Table 23-1. JEditorPane properties
The
contentType
property reflects the type of content displayed by the editor. This value is taken from the installed
EditorKit
and typically has values such as
"text/plain"
,
"text/html"
, and
"text/rtf"
. The
editorKit
A custom FocusTraversalPolicy property is installed so the Tab key does not move focus to the next component. [2] The page property specifies the URL of the current page being displayed. The scrollableTracksViewportWidth property is true for this class.
23.1.2 Events
JEditorPane
s fire a special type of event called a
HyperlinkEvent
, which is typically
As you'd expect, the following methods are provided for working with these events:
JEditorPane
objects also fire
PropertyChangeEvent
s when the
editorKit
property is changed.
getHyperlinkListeners( )
was introduced in SDK 1.4. While not restricted to HTML documents by any means, HTML is a natural and familiar environment for hyperlinks (so we have
23.1.3 ConstructorsThe following constructors are provided. Note that the last two may throw an IOException if they are unable to load the specified URL (including if the server returns an HTTP error).
23.1.4 EditorKit MethodsThe following methods are available for managing EditorKit s. You won't need to use any of these methods unless you're defining your own EditorKit s for working with various content types.
Table 23-2. Default content type mappings
23.1.5 Miscellaneous Methods
|