APIdia Launch Partner
Top Description Inners Fields Constructors Methods
javax.swing

public Class JEditorPane

extends JTextComponent
Class Inheritance
Known Direct Subclasses
javax.swing.JTextPane, javax.swing.text.html.FrameView.FrameEditorPane
Annotations
@JavaBean
defaultProperty:UIClassID
description:A text component to edit various types of content.
@SwingContainer:false
@SuppressWarnings:serial
Imports
java.awt.Component, .Container, .Dimension, .Graphics, .IllegalComponentStateException, .Point, .Rectangle, .Shape, java.beans.BeanProperty, .JavaBean, java.io.BufferedInputStream, .DataOutputStream, .IOException, .InputStream, .InputStreamReader, .ObjectOutputStream, .OutputStream, .Reader, .Serial, .StringReader, .StringWriter, java.lang.reflect.InvocationTargetException, java.net.HttpURLConnection, .MalformedURLException, .URL, .URLConnection, java.util.Enumeration, .HashMap, .Hashtable, .Map, .Vector, javax.accessibility.Accessible, .AccessibleComponent, .AccessibleContext, .AccessibleHyperlink, .AccessibleHypertext, .AccessibleState, .AccessibleStateSet, .AccessibleText, javax.swing.event.DocumentEvent, .DocumentListener, .EventListenerList, .HyperlinkEvent, .HyperlinkListener, javax.swing.plaf.TextUI, javax.swing.text.AbstractDocument, .AttributeSet, .BadLocationException, .BoxView, .Caret, .ChangedCharSetException, .CompositeView, .DefaultEditorKit, .Document, .EditorKit, .Element, .ElementIterator, .GlyphView, .JTextComponent, .StyleConstants, .StyledEditorKit, .View, .ViewFactory, .WrappedPlainView, javax.swing.text.html.HTML, .HTMLDocument, .HTMLEditorKit

A text component to edit various kinds of content. You can find how-to information and examples of using editor panes in Using Text Components, a section in The Java Tutorial.

This component uses implementations of the EditorKit to accomplish its behavior. It effectively morphs into the proper kind of text editor for the kind of content it is given. The content type that editor is bound to at any given time is determined by the EditorKit currently installed. If the content is set to a new URL, its type is used to determine the EditorKit that should be used to load the content.

By default, the following types of content are known:

text/plain
Plain text, which is the default the type given isn't recognized. The kit used in this case is an extension of DefaultEditorKit that produces a wrapped plain text view.
text/html
HTML text. The kit used in this case is the class javax.swing.text.html.HTMLEditorKit which provides HTML 3.2 support.
text/rtf
RTF text. The kit used in this case is the class javax.swing.text.rtf.RTFEditorKit which provides a limited support of the Rich Text Format.

There are several ways to load content into this component.

  1. The setText method can be used to initialize the component from a string. In this case the current EditorKit will be used, and the content type will be expected to be of this type.
  2. The read method can be used to initialize the component from a Reader. Note that if the content type is HTML, relative references (e.g. for things like images) can't be resolved unless the <base> tag is used or the Base property on HTMLDocument is set. In this case the current EditorKit will be used, and the content type will be expected to be of this type.
  3. The setPage method can be used to initialize the component from a URL. In this case, the content type will be determined from the URL, and the registered EditorKit for that content type will be set.

Some kinds of content may provide hyperlink support by generating hyperlink events. The HTML EditorKit will generate hyperlink events if the JEditorPane is not editable (JEditorPane.setEditable(false); has been called). If HTML frames are embedded in the document, the typical response would be to change a portion of the current document. The following code fragment is a possible hyperlink listener implementation, that treats HTML frame events specially, and simply displays any other activated hyperlinks.


     class Hyperactive implements HyperlinkListener {
 
         public void hyperlinkUpdate(HyperlinkEvent e) {
             if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
                 JEditorPane pane = (JEditorPane) e.getSource();
                 if (e instanceof HTMLFrameHyperlinkEvent) {
                     HTMLFrameHyperlinkEvent  evt = (HTMLFrameHyperlinkEvent)e;
                     HTMLDocument doc = (HTMLDocument)pane.getDocument();
                     doc.processHTMLFrameHyperlinkEvent(evt);
                 } else {
                     try {
                         pane.setPage(e.getURL());
                     } catch (Throwable t) {
                         t.printStackTrace();
                     }
                 }
             }
         }
     }

 

For information on customizing how text/html is rendered please see W3C_LENGTH_UNITS and HONOR_DISPLAY_PROPERTIES

Culturally dependent information in some documents is handled through a mechanism called character encoding. Character encoding is an unambiguous mapping of the members of a character set (letters, ideographs, digits, symbols, or control functions) to specific numeric code values. It represents the way the file is stored. Example character encodings are ISO-8859-1, ISO-8859-5, Shift-jis, Euc-jp, and UTF-8. When the file is passed to an user agent (JEditorPane) it is converted to the document character set (ISO-10646 aka Unicode).

There are multiple ways to get a character set mapping to happen with JEditorPane.

  1. One way is to specify the character set as a parameter of the MIME type. This will be established by a call to the setContentType method. If the content is loaded by the setPage method the content type will have been set according to the specification of the URL. It the file is loaded directly, the content type would be expected to have been set prior to loading.
  2. Another way the character set can be specified is in the document itself. This requires reading the document prior to determining the character set that is desired. To handle this, it is expected that the EditorKit.read operation throw a ChangedCharSetException which will be caught. The read is then restarted with a new Reader that uses the character set specified in the ChangedCharSetException (which is an IOException).
Newlines
For a discussion on how newlines are handled, see DefaultEditorKit.

Warning

Swing is not thread safe. For more information see Swing's Threading Policy.

Warning

Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. As of 1.4, support for long term storage of all JavaBeans has been added to the java.beans package. Please see java.beans.XMLEncoder.

Author
Timothy Prinzing
Since
1.2

Nested and Inner Type Summary

Modifier and TypeClass and Description
protected class
JEditorPane.AccessibleJEditorPane

This class implements accessibility support for the JEditorPane class.

protected class
JEditorPane.AccessibleJEditorPaneHTML

This class provides support for AccessibleHypertext, and is used in instances where the EditorKit installed in this JEditorPane is an instance of HTMLEditorKit.

pack-priv static class
protected class
JEditorPane.JEditorPaneAccessibleHypertextSupport

What's returned by AccessibleJEditorPaneHTML.getAccessibleText.

pack-priv class
JEditorPane.PageLoader

Loads a stream into the text document model.

pack-priv static class

Field Summary

Modifier and TypeField and Description
pack-priv static final Map<String, String>
public static final String
HONOR_DISPLAY_PROPERTIES

Key for a client property used to indicate whether the default font and foreground color from the component are used if a font or foreground color is not specified in the styled text.

private boolean
private EditorKit
kit

Current content binding of the editor.

private static final Hashtable<String, ClassLoader>
private static final Hashtable<String, EditorKit>
private static final Hashtable<String, String>
private SwingWorker<URL, Object>
private Hashtable<String, Object>
pack-priv static final String
PostDataProperty

Should be kept in sync with javax.swing.text.html.FormView counterpart.

private Hashtable<String, EditorKit>
typeHandlers

Table of registered type handlers for this editor.

private static final String
public static final String
W3C_LENGTH_UNITS

Key for a client property used to indicate whether w3c compliant length units are used for html rendering.

Inherited from javax.swing.text.JTextComponent:
DEFAULT_KEYMAPFOCUS_ACCELERATOR_KEY

Constructor Summary

AccessConstructor and Description
public
JEditorPane()

Creates a new JEditorPane.

public
JEditorPane(URL
the URL
initialPage
)

Creates a JEditorPane based on a specified URL for input.

public
JEditorPane(String
the URL
url
)

Creates a JEditorPane based on a string containing a URL specification.

public
JEditorPane(String
mime type of the given text
type
,
String
the text to initialize with; may be null
text
)

Creates a JEditorPane that has been initialized to the given text.

Method Summary

Modifier and TypeMethod and Description
public synchronized void
addHyperlinkListener(HyperlinkListener
the listener
listener
)

Adds a hyperlink listener for notification of any changes, for example when a link is selected and entered.

protected EditorKit

Returns:

the editor kit
createDefaultEditorKit
()

Creates the default editor kit (PlainEditorKit) for when the component is first created.

public static EditorKit

Returns:

the editor kit, or null if there is nothing registered for the given type
createEditorKitForContentType
(String
the content type
type
)

Creates a handler for the given type from the default registry of editor kits.

public void
fireHyperlinkUpdate(HyperlinkEvent
the event
e
)

Notifies all listeners that have registered interest for notification on this event type.

public AccessibleContext

Returns:

an AccessibleJEditorPane that serves as the AccessibleContext of this JEditorPane
getAccessibleContext
()

Overrides javax.swing.text.JTextComponent.getAccessibleContext.

Implements javax.accessibility.Accessible.getAccessibleContext.

Gets the AccessibleContext associated with this JEditorPane.

private int
getAsynchronousLoadPriority(Document doc)

Return load priority for the document or -1 if priority not supported.

public final String

Returns:

the content type, null if no editor kit set
getContentType
()

Gets the type of content that this editor is currently set to deal with.

public EditorKit

Returns:

the editor kit
getEditorKit
()

Fetches the currently installed kit for handling content.

public static String

Returns:

a String containing the EditorKit class name for type
getEditorKitClassNameForContentType
(String
the non-null content type
type
)

Returns the currently registered EditorKit class name for the type type.

public EditorKit

Returns:

the editor kit
getEditorKitForContentType
(String
the non-null content type
type
)

Fetches the editor kit to use for the given type of content.

public synchronized HyperlinkListener[]

Returns:

all of the HyperLinkListeners added or an empty array if no listeners have been added
getHyperlinkListeners
()

Returns an array of all the HyperLinkListeners added to this JEditorPane with addHyperlinkListener().

public URL

Returns:

the URL, or null if none
getPage
()

Gets the current URL being displayed.

private Object
public Dimension

Returns:

a Dimension containing the preferred size
getPreferredSize
()

Overrides javax.swing.JComponent.getPreferredSize.

Returns the preferred size for the JEditorPane.

public boolean

Returns:

true if a viewport should force the Scrollable's height to match its own, false otherwise
getScrollableTracksViewportHeight
()

Overrides javax.swing.text.JTextComponent.getScrollableTracksViewportHeight.

Implements javax.swing.Scrollable.getScrollableTracksViewportHeight.

Returns true if a viewport should always force the height of this Scrollable to match the height of the viewport.

public boolean

Returns:

true if a viewport should force the Scrollables width to match its own, false otherwise
getScrollableTracksViewportWidth
()

Overrides javax.swing.text.JTextComponent.getScrollableTracksViewportWidth.

Implements javax.swing.Scrollable.getScrollableTracksViewportWidth.

Returns true if a viewport should always force the width of this Scrollable to match the width of the viewport.

protected InputStream

Returns:

a stream for the URL which is about to be loaded
getStream
(URL
the URL of the page
page
)

Fetches a stream for the given URL, which is about to be loaded by the setPage method.

public String

Returns:

the text
getText
()

Overrides javax.swing.text.JTextComponent.getText.

Returns the text contained in this TextComponent in terms of the content type of this editor.

public String

Returns:

the string "EditorPaneUI"
getUIClassID
()

Overrides javax.swing.JComponent.getUIClassID.

Gets the class ID for the UI.

private void
handleConnectionProperties(URLConnection conn)

Handle URL connection properties (most notably, content type).

private void
private Document
initializeModel(EditorKit kit, URL page)

Create model and initialize document properties from page properties.

protected String

Returns:

a string representation of this JEditorPane
paramString
()

Overrides javax.swing.text.JTextComponent.paramString.

Returns a string representation of this JEditorPane.

public void
read(InputStream
the stream from which to read
in
,
Object
an object describing the stream
desc
)

This method initializes from a stream.

pack-priv void
read(InputStream
the inputstream to use
in
,
Document
the document to load
doc
)

This method invokes the EditorKit to initiate a read.

public static void
registerEditorKitForContentType(String
the non-null content type
type
,
String
the class to load later
classname
)

Establishes the default bindings of type to classname.

public static void
registerEditorKitForContentType(String
the non-null content type
type
,
String
the class to load later
classname
,
ClassLoader
the ClassLoader to use to load the name
loader
)

Establishes the default bindings of type to classname.

public synchronized void
removeHyperlinkListener(HyperlinkListener
the listener
listener
)

Removes a hyperlink listener.

public void
replaceSelection(String
the content to replace the selection with. This value can be null
content
)

Overrides javax.swing.text.JTextComponent.replaceSelection.

Replaces the currently selected content with new content represented by the given string.

public void
scrollToReference(String
the named location to scroll to
reference
)

Scrolls the view to the given reference location (that is, the value returned by the URL.getRef method for the URL being displayed).

private void
setCharsetFromContentTypeParameters(String paramlist)

This method gets the charset information specified as part of the content type in the http header information.

public final void
setContentType(String
the non-null mime type for the content editing support
type
)

Sets the type of content that this editor handles.

public void
setEditorKit(EditorKit
the desired editor behavior
kit
)

Sets the currently installed kit for handling content.

public void
setEditorKitForContentType(String
the non-null content type
type
,
EditorKit
the editor kit to be set
k
)

Directly sets the editor kit to use for the given type.

public void
setPage(URL
the URL of the page
page
)

Sets the current URL being displayed.

public void
setPage(String
the URL for display
url
)

Sets the current URL being displayed.

public void
setText(String
the new text to be set; if null the old text will be deleted
t
)

Overrides javax.swing.text.JTextComponent.setText.

Sets the text of this TextComponent to the specified content, which is expected to be in the format of the content type of this editor.

private void
writeObject(ObjectOutputStream s)

Hides javax.swing.JComponent.writeObject.

See readObject and writeObject in JComponent for more information about serialization in Swing.

Inherited from javax.swing.text.JTextComponent:
addCaretListeneraddInputMethodListeneraddKeymapcopycutfireCaretUpdategetActionsgetCaretgetCaretColorgetCaretListenersgetCaretPositiongetDisabledTextColorgetDocumentgetDragEnabledgetDropLocationgetDropModegetFocusAcceleratorgetHighlightergetInputMethodRequestsgetKeymapgetKeymapgetMargingetNavigationFiltergetPreferredScrollableViewportSizegetPrintablegetScrollableBlockIncrementgetScrollableUnitIncrementgetSelectedTextgetSelectedTextColorgetSelectionColorgetSelectionEndgetSelectionStartgetTextgetToolTipTextgetUIisEditableloadKeymapmodelToViewmodelToView2DmoveCaretPositionpasteprintprintprintprocessInputMethodEventreadremoveCaretListenerremoveKeymapremoveNotifyrestoreComposedTextsaveComposedTextselectselectAllsetCaretsetCaretColorsetCaretPositionsetComponentOrientationsetDisabledTextColorsetDocumentsetDragEnabledsetDropModesetEditablesetFocusAcceleratorsetHighlightersetKeymapsetMarginsetNavigationFiltersetSelectedTextColorsetSelectionColorsetSelectionEndsetSelectionStartsetUIupdateUIviewToModelviewToModel2Dwrite