|
JSR-209 (Final Release) | ||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjava.awt.Component
java.awt.Container
javax.swing.JComponent
javax.swing.text.JTextComponent
javax.swing.JEditorPane
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:
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
.
EditorKit
for that content type will be set.
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
).
Note: The following clarification comes from the DefaultEditorKit
class which is removed from this JSR however should still be respected
for upward compatibility.
There are two properties which deal with newlines. The system property, line.separator, is defined to be platform-dependent, either "\n", "\r", or "\r\n". There is also a property called EndOfLineStringProperty, which is defined automatically when a document is loaded, to be the first occurrence of any of the newline characters. When a document is loaded, EndOfLineStringProperty is set appropriately, and when the document is written back out, the EndOfLineStringProperty is used. But while the document is in memory, the "\n" character is used to define a newline, regardless of how the newline is defined when the document is on disk. Therefore, for searching purposes, "\n" should always be used. When a new document is created, and the EndOfLineStringProperty has not been defined, it will use the System property when writing out the document. Note that EndOfLineStringProperty is set on the Document using the get/setProperty methods. Subclasses may override this behavior.
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
.
Nested Class Summary |
Nested classes inherited from class javax.swing.text.JTextComponent |
JTextComponent.KeyBinding |
Field Summary |
Fields inherited from class javax.swing.text.JTextComponent |
DEFAULT_KEYMAP, FOCUS_ACCELERATOR_KEY |
Fields inherited from class javax.swing.JComponent |
listenerList, TOOL_TIP_TEXT_KEY, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW |
Fields inherited from class java.awt.Component |
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT |
Fields inherited from interface java.awt.image.ImageObserver |
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH |
Constructor Summary | |
JEditorPane()
Creates a new JEditorPane . |
|
JEditorPane(java.lang.String url)
Creates a JEditorPane based on a string containing
a URL specification. |
|
JEditorPane(java.lang.String type,
java.lang.String text)
Creates a JEditorPane that has been initialized
to the given text. |
|
JEditorPane(java.net.URL initialPage)
Creates a JEditorPane based on a specified URL for input. |
Method Summary | |
void |
addHyperlinkListener(HyperlinkListener listener)
Adds a hyperlink listener for notification of any changes, for example when a link is selected and entered. |
protected EditorKit |
createDefaultEditorKit()
Creates the default editor kit ( PlainEditorKit ) for when
the component is first created. |
void |
fireHyperlinkUpdate(HyperlinkEvent e)
Notifies all listeners that have registered interest for notification on this event type. |
java.lang.String |
getContentType()
Gets the type of content that this editor is currently set to deal with. |
EditorKit |
getEditorKit()
Fetches the currently installed kit for handling content. |
EditorKit |
getEditorKitForContentType(java.lang.String type)
Fetches the editor kit to use for the given type of content. |
HyperlinkListener[] |
getHyperlinkListeners()
Returns an array of all the HyperLinkListener s added
to this JEditorPane with addHyperlinkListener(). |
java.net.URL |
getPage()
Gets the current URL being displayed. |
Dimension |
getPreferredSize()
Returns the preferred size for the JEditorPane . |
boolean |
getScrollableTracksViewportHeight()
Returns true if a viewport should always force the height of this Scrollable to match the height of the viewport. |
boolean |
getScrollableTracksViewportWidth()
Returns true if a viewport should always force the width of this Scrollable to match the width of the viewport. |
protected java.io.InputStream |
getStream(java.net.URL page)
Fetches a stream for the given URL, which is about to be loaded by the setPage method. |
java.lang.String |
getText()
Returns the text contained in this TextComponent
in terms of the
content type of this editor. |
void |
paint(java.awt.Graphics g)
Invoked by Swing to draw components. |
protected void |
paintBorder(java.awt.Graphics g)
Paints the component's border. |
protected void |
paintComponent(java.awt.Graphics g)
Calls the UI delegate's paint method, if the UI delegate is non- null . |
void |
paintComponents(java.awt.Graphics g)
|
protected java.lang.String |
paramString()
Returns a string representation of this JEditorPane . |
protected void |
printBorder(java.awt.Graphics g)
Prints the component's border. |
protected void |
printComponent(java.awt.Graphics g)
This is invoked during a printing operation. |
void |
printComponents(java.awt.Graphics g)
|
void |
read(java.io.InputStream in,
java.lang.Object desc)
This method initializes from a stream. |
void |
removeHyperlinkListener(HyperlinkListener listener)
Removes a hyperlink listener. |
void |
replaceSelection(java.lang.String content)
Replaces the currently selected content with new content represented by the given string. |
void |
scrollToReference(java.lang.String reference)
Scrolls the view to the given reference location (that is, the value returned by the UL.getRef
method for the URL being displayed). |
void |
setContentType(java.lang.String type)
Sets the type of content that this editor handles. |
void |
setEditorKit(EditorKit kit)
Sets the currently installed kit for handling content. |
void |
setEditorKitForContentType(java.lang.String type,
EditorKit k)
Directly sets the editor kit to use for the given type. |
void |
setPage(java.lang.String url)
Sets the current URL being displayed. |
void |
setPage(java.net.URL page)
Sets the current URL being displayed. |
void |
setText(java.lang.String t)
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. |
Methods inherited from class java.awt.Container |
add, add, add, add, add, addContainerListener, addImpl, areFocusTraversalKeysSet, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicySet, list, list, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setFocusCycleRoot, setFocusTraversalKeys, setFocusTraversalPolicy, setLayout, transferFocusBackward, transferFocusDownCycle, validate, validateTree |
Methods inherited from class java.awt.Component |
addComponentListener, addFocusListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enableEvents, enableInputMethods, getBackground, getBounds, getColorModel, getComponentListeners, getCursor, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphicsConfiguration, getIgnoreRepaint, getInputContext, getInputMethodListeners, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMouseWheelListeners, getName, getParent, getSize, getToolkit, getTreeLock, hasFocus, imageUpdate, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isShowing, isValid, isVisible, list, list, list, paintAll, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processMouseEvent, processMouseWheelEvent, removeComponentListener, removeFocusListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, repaint, repaint, repaint, setBounds, setBounds, setCursor, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, toString, transferFocus, transferFocusUpCycle |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
public JEditorPane()
JEditorPane
.
The document model is set to null
.
public JEditorPane(java.net.URL initialPage) throws java.io.IOException
JEditorPane
based on a specified URL for input.
initialPage
- the URL
java.io.IOException
- if the URL is null
or cannot be accessedpublic JEditorPane(java.lang.String url) throws java.io.IOException
JEditorPane
based on a string containing
a URL specification.
url
- the URL
java.io.IOException
- if the URL is null
or
cannot be accessedpublic JEditorPane(java.lang.String type, java.lang.String text)
JEditorPane
that has been initialized
to the given text. This is a convenience constructor that calls the
setContentType
and setText
methods.
type
- mime type of the given texttext
- the text to initialize with
java.lang.NullPointerException
- if the type
parameter
is null
Method Detail |
public void addHyperlinkListener(HyperlinkListener listener)
listener
- the listenerpublic void removeHyperlinkListener(HyperlinkListener listener)
listener
- the listenerpublic HyperlinkListener[] getHyperlinkListeners()
HyperLinkListener
s added
to this JEditorPane with addHyperlinkListener().
HyperLinkListener
s added or an empty
array if no listeners have been addedpublic void fireHyperlinkUpdate(HyperlinkEvent e)
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.
e
- the eventEventListenerList
public void setPage(java.net.URL page) throws java.io.IOException
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 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 EditorKit
.
If the Document
is of type
AbstractDocument
and has a value returned by
AbstractDocument.getAsynchronousLoadPriority
that is greater than or equal to zero, the page will be
loaded on a separate thread using that priority.
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 setDocument
, which
is bound and will fire a property change event. If an
IOException
is thrown the partially loaded
document will
be discarded and neither the document or page property
change events will be fired. If the document is
successfully loaded and installed, a view will be
built for it by the UI which will then be scrolled if
necessary, and then the page property change event
will be fired.
If the document is loaded asynchronously, the document
will be installed into the editor immediately using a
call to setDocument
which will fire a
document property change event, then a thread will be
created which will begin doing the actual loading.
In this case, the page property change event will not be
fired by the call to this method directly, but rather will be
fired when the thread doing the loading has finished.
It will also be fired on the event-dispatch thread.
Since the calling thread can not throw an IOException
in the event of failure on the other thread, the page
property change event will be fired when the other
thread is done whether the load was successful or not.
page
- the URL of the page
java.io.IOException
- for a null
or invalid
page specification, or exception from the stream being readgetPage()
public void read(java.io.InputStream in, java.lang.Object desc) throws java.io.IOException
in
- the stream from which to readdesc
- an object describing the stream
java.io.IOException
- as thrown by the stream being
used to initializeJTextComponent.read(java.io.Reader, java.lang.Object)
,
JTextComponent.setDocument(javax.swing.text.Document)
protected java.io.InputStream getStream(java.net.URL page) throws java.io.IOException
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 EditorKit
to use for loading the stream.
If this the stream was an http connection, redirects
will be followed and the resulting URL will be set as
the Document.StreamDescriptionProperty
so that relative
URL's can be properly resolved.
page
- the URL of the page
java.io.IOException
public void scrollToReference(java.lang.String reference)
UL.getRef
method for the URL being displayed). 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.
reference
- the named location to scroll topublic java.net.URL getPage()
null
, and relative URL's will not be
resolved.
null
if nonepublic void setPage(java.lang.String url) throws java.io.IOException
url
- the URL for display
java.io.IOException
- for a null
or invalid URL
specificationprotected EditorKit createDefaultEditorKit()
PlainEditorKit
) for when
the component is first created.
public EditorKit getEditorKit()
createDefaultEditorKit
is called to set up a default
if necessary.
public final java.lang.String getContentType()
EditorKit
.
null
if no editor kit setpublic final void setContentType(java.lang.String type)
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 EditorKit
.
For example if the type is specified as
text/html; charset=EUC-JP
the content
will be loaded using the EditorKit
registered for
text/html
and the Reader provided to
the EditorKit
to load unicode into the document will
use the EUC-JP
charset for translating
to unicode. If the type is not recognized, the content
will be loaded using the EditorKit
registered
for plain text, text/plain
.
type
- the non-null
mime type for the content editing
support
java.lang.NullPointerException
- if the type
parameter
is null
getContentType()
public void setEditorKit(EditorKit kit)
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 EditorKit
is the source of how a
particular type
of content is modeled. This method will cause setDocument
to be called on behalf of the caller to ensure integrity
of the internal state.
kit
- the desired editor behaviorgetEditorKit()
public EditorKit getEditorKitForContentType(java.lang.String type)
type
- the non-null content type
public void setEditorKitForContentType(java.lang.String type, EditorKit k)
type
- the non-null
content typek
- the editor kit to be setpublic void replaceSelection(java.lang.String content)
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.
This method is thread safe, although most Swing methods are not. Please see Threads and Swing for more information.
replaceSelection
in class JTextComponent
content
- the content to replace the selection with. This
value can be null
public Dimension getPreferredSize()
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.
getPreferredSize
in class JComponent
Dimension
containing the preferred sizeJComponent.setPreferredSize(java.awt.Dimension)
,
ComponentUI
public void setText(java.lang.String t)
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
EditorKit
. This gives the semantics of the
superclass by not changing
out the model, while supporting the content type currently set on
this component. The assumption is that the previous content is
relatively
small, and that the previous content doesn't have side effects.
Both of those assumptions can be violated and cause undesirable results.
To avoid this, create a new document,
getEditorKit().createDefaultDocument()
, and replace the
existing Document
with the new one. You are then assured the
previous Document
won't have any lingering state.
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.
This method is thread safe, although most Swing methods are not. Please see Threads and Swing for more information.
setText
in class JTextComponent
t
- the new text to be setgetText()
public java.lang.String getText()
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
.
getText
in class JTextComponent
setText(java.lang.String)
public boolean getScrollableTracksViewportWidth()
Scrollable
to match the width of the viewport.
getScrollableTracksViewportWidth
in interface Scrollable
getScrollableTracksViewportWidth
in class JTextComponent
public boolean getScrollableTracksViewportHeight()
Scrollable
to match the height of the viewport.
getScrollableTracksViewportHeight
in interface Scrollable
getScrollableTracksViewportHeight
in class JTextComponent
Scrollable
's height to match its own,
false otherwiseprotected java.lang.String paramString()
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
.
paramString
in class JTextComponent
JEditorPane
protected final void paintComponent(java.awt.Graphics g)
JComponent
null
. We pass the delegate a copy of the
Graphics
object to protect the rest of the
paint code from irrevocable changes
(for example, Graphics.translate
).
paintComponent
in class JComponent
g
- the Graphics
object to protectJComponent.paint(java.awt.Graphics)
,
ComponentUI
protected final void paintBorder(java.awt.Graphics g)
JComponent
paintBorder
in class JComponent
g
- the Graphics
context in which to paintJComponent.paint(java.awt.Graphics)
,
JComponent.setBorder(javax.swing.border.Border)
public final void paintComponents(java.awt.Graphics g)
public final void paint(java.awt.Graphics g)
JComponent
paint
directly,
but should instead use the repaint
method to
schedule the component for redrawing.
This method actually delegates the work of painting to three
protected methods: paintComponent
,
paintBorder
,
and paintChildren
. They're called in the order
listed to ensure that children appear on top of component itself.
Generally speaking, the component and its children should not
paint in the insets area allocated to the border. Subclasses can
just override this method, as always.
paint
in class JComponent
g
- the Graphics
context in which to paintJComponent.paintBorder(java.awt.Graphics)
,
JComponent.paintChildren(java.awt.Graphics)
,
JComponent.getComponentGraphics(java.awt.Graphics)
,
JComponent.repaint(long, int, int, int, int)
protected final void printComponent(java.awt.Graphics g)
JComponent
paintComponent
on the component.
printComponent
in class JComponent
g
- the Graphics
context in which to paintJComponent.print(java.awt.Graphics)
protected final void printBorder(java.awt.Graphics g)
JComponent
paintBorder
on the component.
printBorder
in class JComponent
g
- the Graphics
context in which to paintJComponent.print(java.awt.Graphics)
public final void printComponents(java.awt.Graphics g)
|
JSR-209 (Final Release) | ||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |