java.lang.Objectjava.awt.Component
java.awt.Container
javax.swing.JComponent
javax.swing.text.JTextComponent
javax.swing.JEditorPane
All Implemented Interfaces:
Scrollable, Accessible, HasGetTransferHandler, Serializable, MenuContainer, ImageObserver
Direct Known Subclasses:
JTextPane, HelpContent, FrameEditorPane
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:
DefaultEditorKit
that produces a wrapped plain text view.
javax.swing.text.html.HTMLEditorKit
which provides HTML 3.2 support.
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.
EditorKit
will be used, and the content type will be
expected to be of this type.
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.
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
.
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
).
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 JavaBeansTM
has been added to the java.beans
package.
Please see java.beans.XMLEncoder .
attribute
- : isContainer false
description: A text component to edit various types of content.Timothy
- PrinzingNested Class Summary: | ||
---|---|---|
class | JEditorPane.PageLoader | Loads a stream into the text document model. |
protected class | JEditorPane.AccessibleJEditorPane | This class implements accessibility support for the
JEditorPane class. It provides an implementation of the
Java Accessibility API appropriate to editor pane user-interface
elements.
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 JavaBeansTM
has been added to the |
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 .
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 JavaBeansTM
has been added to the |
protected class | JEditorPane.JEditorPaneAccessibleHypertextSupport | What's returned by
AccessibleJEditorPaneHTML.getAccessibleText .
Provides support for AccessibleHypertext in case
there is an HTML document being displayed in this
JEditorPane . |
static class | JEditorPane.PlainEditorKit | |
static class | JEditorPane.HeaderParser |
Field Summary | ||
---|---|---|
static final String | PostDataProperty | Should be kept in sync with javax.swing.text.html.FormView counterpart. |
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.
By default this is not enabled; to enable
it set the client property with this name
to
|
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.
The default varies based on the look and feel;
to enable it set the client property with
this name to
|
static final Map<String, String> | defaultEditorKitMap |
Fields inherited from javax.swing.text.JTextComponent: |
---|
FOCUS_ACCELERATOR_KEY, DEFAULT_KEYMAP |
Methods from java.lang.Object: |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Method from javax.swing.JEditorPane Detail: |
---|
|
PlainEditorKit ) for when
the component is first created. |
ClassLoader ,
that ClassLoader will be used to load the prototype.
If there was no registered ClassLoader ,
Class.forName will be used to load the prototype.
Once a prototype |
EditorKit if a content type
that supports hyperlinks is currently active and there
was activity with a link. The listener list is processed
last to first. |
|
EditorKit . |
createDefaultEditorKit is called to set up a default
if necessary. |
EditorKit
class name for the type type . |
EditorKit registered
for the given type, it will try to create an
EditorKit from the default EditorKit registry.
If that fails, a PlainEditorKit is used on the
assumption that all text documents can be represented
as plain text.
This method can be reimplemented to use some other kind of type registry. This can be reimplemented to use the Java Activation Framework, for example. |
HyperLinkListener s added
to this JEditorPane with addHyperlinkListener(). |
null , and relative URL's will not be
resolved. |
JEditorPane .
The preferred size for JEditorPane is slightly altered
from the preferred size of the superclass. If the size
of the viewport has become smaller than the minimum size
of the component, the scrollable definition for tracking
width or height will turn to false. The default viewport
layout will give the preferred size, and that is not desired
in the case where the scrollable is tracking. In that case
the normal preferred size is adjusted to the
minimum size. This allows things like HTML tables to
shrink down to their minimum size and then be laid out at
their minimum size, refusing to shrink any further. |
Scrollable to match the height of the viewport. |
Scrollable to match the width of the viewport. |
setPage method. By
default, this simply opens the URL and returns the
stream. This can be reimplemented to do useful things
like fetch the stream from a cache, monitor the progress
of the stream, etc.
This method is expected to have the the side effect of
establishing the content type, and therefore setting the
appropriate
If this the stream was an http connection, redirects
will be followed and the resulting URL will be set as
the |
TextComponent
in terms of the
content type of this editor. If an exception is thrown while
attempting to retrieve the text, null will be returned.
This is implemented to call JTextComponent.write with
a StringWriter . |
|
JEditorPane .
This method
is intended to be used only for debugging purposes, and the
content and format of the returned string may vary between
implementations. The returned string may be empty but may not
be null . |
HTMLEditorKit , and the
desc parameter is an HTMLDocument ,
then it invokes the HTMLEditorKit to initiate
the read. Otherwise it calls the superclass
method which loads the model as plain text. |
EditorKit to initiate a
read. In the case where a ChangedCharSetException
is thrown this exception will contain the new CharSet.
Therefore the read operation
is then restarted after building a new Reader with the new charset. |
type to
classname .
The class will be dynamically loaded later when actually
needed, and can be safely changed before attempted uses
to avoid loading unwanted classes. The prototype
EditorKit will be loaded with Class.forName
when registered with this method. |
type to
classname .
The class will be dynamically loaded later when actually
needed using the given ClassLoader ,
and can be safely changed
before attempted uses to avoid loading unwanted classes. |
|
null ) this amounts to a removal of the
current selection. The replacement text will have the
attributes currently defined for input. If the component is not
editable, beep and return. |
UL.getRef
method for the URL being displayed). By default, this
method only knows how to locate a reference in an
HTMLDocument. The implementation calls the
scrollRectToVisible method to
accomplish the actual scrolling. If scrolling to a
reference location is needed for document types other
than HTML, this method should be reimplemented.
This method will have no effect if the component
is not visible. |
getEditorKitForContentType ,
and then setEditorKit if an editor kit can
be successfully located. This is mostly convenience method
that can be used as an alternative to calling
setEditorKit directly.
If there is a charset definition specified as a parameter
of the content type specification, it will be used when
loading input streams using the associated |
null ,
the new kit is installed, and a default document created for it.
A PropertyChange event ("editorKit") is always fired when
setEditorKit is called.
NOTE: This has the side effect of changing the model,
because the |
createEditorKitForContentType to install handlers for
content types with a look-and-feel bias. |
null , then
a new default document is created and the URL is read into it.
If the URL contains and reference location, the location will
be scrolled to by calling the scrollToReference
method. If the desired URL is the one currently being displayed,
the document will not be reloaded. To force a document
reload it is necessary to clear the stream description property
of the document. The following code shows how this can be done:
Document doc = jEditorPane.getDocument(); doc.putProperty(Document.StreamDescriptionProperty, null);If the desired URL is not the one currently being displayed, the getStream method is called to
give subclasses control over the stream provided.
This may load either synchronously or asynchronously
depending upon the document returned by the
If the document is loaded synchronously, it will be
filled in with the stream prior to being installed into
the editor with a call to
If the document is loaded asynchronously, the document
will be installed into the editor immediately using a
call to |
|
TextComponent to the specified
content,
which is expected to be in the format of the content type of
this editor. For example, if the type is set to text/html
the string should be specified in terms of HTML.
This is implemented to remove the contents of the current document,
and replace them by parsing the given string using the current
An alternative way to load this component with a string would be to create a StringReader and call the read method. In this case the model would be replaced after it was initialized with the contents of the string. |