JEditorPane (Java 2 Platform SE 5.0) (original) (raw)
A text component to edit various kinds of content. You can find how-to information and examples of using editor panes inUsing Text Components, a section in The Java Tutorial.
This component uses implementations of theEditorKit
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.
There are several ways to load content into this component.
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.``
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
.
For a discussion on how newlines are handled, seeDefaultEditorKit.
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 XMLEncoder.
Nested Class Summary | |
---|---|
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 ofHTMLEditorKit. |
protected class | JEditorPane.JEditorPaneAccessibleHypertextSupport What's returned byAccessibleJEditorPaneHTML.getAccessibleText. |
Nested classes/interfaces inherited from class javax.swing.text.JTextComponent |
---|
JTextComponent.AccessibleJTextComponent, JTextComponent.KeyBinding |
Nested classes/interfaces inherited from class javax.swing.JComponent |
---|
JComponent.AccessibleJComponent |
Nested classes/interfaces inherited from class java.awt.Container |
---|
Container.AccessibleAWTContainer |
Nested classes/interfaces inherited from class java.awt.Component |
---|
Component.AccessibleAWTComponent, Component.BltBufferStrategy, Component.FlipBufferStrategy |
Field Summary | |
---|---|
static 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. |
static String | W3C_LENGTH_UNITS Key for a client property used to indicate whether w3c compliant length units are used for html rendering. |
Fields inherited from class javax.swing.text.JTextComponent |
---|
DEFAULT_KEYMAP, FOCUS_ACCELERATOR_KEY |
Fields inherited from class javax.swing.JComponent |
---|
accessibleContext, listenerList, TOOL_TIP_TEXT_KEY, ui, 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(String url) Creates a JEditorPane based on a string containing a URL specification. |
[JEditorPane](../../javax/swing/JEditorPane.html#JEditorPane%28java.lang.String, java.lang.String%29)(String type,String text) Creates a JEditorPane that has been initialized to the given text. |
JEditorPane(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. |
static EditorKit | createEditorKitForContentType(String type) Creates a handler for the given type from the default registry of editor kits. |
void | fireHyperlinkUpdate(HyperlinkEvent e) Notifies all listeners that have registered interest for notification on this event type. |
AccessibleContext | getAccessibleContext() Gets the AccessibleContext associated with this JEditorPane. |
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. |
static String | getEditorKitClassNameForContentType(String type) Returns the currently registered EditorKit class name for the type type. |
EditorKit | getEditorKitForContentType(String type) Fetches the editor kit to use for the given type of content. |
HyperlinkListener[] | getHyperlinkListeners() Returns an array of all the HyperLinkListeners added to this JEditorPane with addHyperlinkListener(). |
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 InputStream | getStream(URL page) Fetches a stream for the given URL, which is about to be loaded by the setPage method. |
String | getText() Returns the text contained in this TextComponent in terms of the content type of this editor. |
String | getUIClassID() Gets the class ID for the UI. |
protected String | paramString() Returns a string representation of this JEditorPane. |
void | [read](../../javax/swing/JEditorPane.html#read%28java.io.InputStream, java.lang.Object%29)(InputStream in,Object desc) This method initializes from a stream. |
static void | [registerEditorKitForContentType](../../javax/swing/JEditorPane.html#registerEditorKitForContentType%28java.lang.String, java.lang.String%29)(String type,String classname) Establishes the default bindings of type toclassname. |
static void | [registerEditorKitForContentType](../../javax/swing/JEditorPane.html#registerEditorKitForContentType%28java.lang.String, java.lang.String, java.lang.ClassLoader%29)(String type,String classname,ClassLoader loader) Establishes the default bindings of type toclassname. |
void | removeHyperlinkListener(HyperlinkListener listener) Removes a hyperlink listener. |
void | replaceSelection(String content) Replaces the currently selected content with new content represented by the given string. |
void | scrollToReference(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(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](../../javax/swing/JEditorPane.html#setEditorKitForContentType%28java.lang.String, javax.swing.text.EditorKit%29)(String type,EditorKit k) Directly sets the editor kit to use for the given type. |
void | setPage(String url) Sets the current URL being displayed. |
void | setPage(URL page) Sets the current URL being displayed. |
void | setText(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 javax.swing.text.JTextComponent |
---|
addCaretListener, addInputMethodListener, [addKeymap](../../javax/swing/text/JTextComponent.html#addKeymap%28java.lang.String, javax.swing.text.Keymap%29), copy, cut, fireCaretUpdate, getActions, getCaret, getCaretColor, getCaretListeners, getCaretPosition, getDisabledTextColor, getDocument, getDragEnabled, getFocusAccelerator, getHighlighter, getInputMethodRequests, getKeymap, getKeymap, getMargin, getNavigationFilter, getPreferredScrollableViewportSize, [getScrollableBlockIncrement](../../javax/swing/text/JTextComponent.html#getScrollableBlockIncrement%28java.awt.Rectangle, int, int%29), [getScrollableUnitIncrement](../../javax/swing/text/JTextComponent.html#getScrollableUnitIncrement%28java.awt.Rectangle, int, int%29), getSelectedText, getSelectedTextColor, getSelectionColor, getSelectionEnd, getSelectionStart, [getText](../../javax/swing/text/JTextComponent.html#getText%28int, int%29), getToolTipText, getUI, isEditable, [loadKeymap](../../javax/swing/text/JTextComponent.html#loadKeymap%28javax.swing.text.Keymap, javax.swing.text.JTextComponent.KeyBinding[], javax.swing.Action[]%29), modelToView, moveCaretPosition, paste, processInputMethodEvent, [read](../../javax/swing/text/JTextComponent.html#read%28java.io.Reader, java.lang.Object%29), removeCaretListener, removeKeymap, removeNotify, [select](../../javax/swing/text/JTextComponent.html#select%28int, int%29), selectAll, setCaret, setCaretColor, setCaretPosition, setComponentOrientation, setDisabledTextColor, setDocument, setDragEnabled, setEditable, setFocusAccelerator, setHighlighter, setKeymap, setMargin, setNavigationFilter, setSelectedTextColor, setSelectionColor, setSelectionEnd, setSelectionStart, setUI, updateUI, viewToModel, write |
Methods inherited from class javax.swing.JComponent |
---|
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, [contains](../../javax/swing/JComponent.html#contains%28int, int%29), createToolTip, disable, enable, [firePropertyChange](../../javax/swing/JComponent.html#firePropertyChange%28java.lang.String, boolean, boolean%29), [firePropertyChange](../../javax/swing/JComponent.html#firePropertyChange%28java.lang.String, char, char%29), [firePropertyChange](../../javax/swing/JComponent.html#firePropertyChange%28java.lang.String, int, int%29), [fireVetoableChange](../../javax/swing/JComponent.html#fireVetoableChange%28java.lang.String, java.lang.Object, java.lang.Object%29), getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintComponent, [paintImmediately](../../javax/swing/JComponent.html#paintImmediately%28int, int, int, int%29), paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, [processKeyBinding](../../javax/swing/JComponent.html#processKeyBinding%28javax.swing.KeyStroke, java.awt.event.KeyEvent, int, boolean%29), processKeyEvent, processMouseEvent, processMouseMotionEvent, [putClientProperty](../../javax/swing/JComponent.html#putClientProperty%28java.lang.Object, java.lang.Object%29), [registerKeyboardAction](../../javax/swing/JComponent.html#registerKeyboardAction%28java.awt.event.ActionListener, javax.swing.KeyStroke, int%29), [registerKeyboardAction](../../javax/swing/JComponent.html#registerKeyboardAction%28java.awt.event.ActionListener, java.lang.String, javax.swing.KeyStroke, int%29), removeAncestorListener, removeVetoableChangeListener, [repaint](../../javax/swing/JComponent.html#repaint%28long, int, int, int, int%29), repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, [reshape](../../javax/swing/JComponent.html#reshape%28int, int, int, int%29), revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, [setFocusTraversalKeys](../../javax/swing/JComponent.html#setFocusTraversalKeys%28int, java.util.Set%29), setFont, setForeground, setInheritsPopupMenu, [setInputMap](../../javax/swing/JComponent.html#setInputMap%28int, javax.swing.InputMap%29), setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update |
Methods inherited from class java.awt.Container |
---|
add, [add](../../java/awt/Container.html#add%28java.awt.Component, int%29), [add](../../java/awt/Container.html#add%28java.awt.Component, java.lang.Object%29), [add](../../java/awt/Container.html#add%28java.awt.Component, java.lang.Object, int%29), [add](../../java/awt/Container.html#add%28java.lang.String, java.awt.Component%29), addContainerListener, [addImpl](../../java/awt/Container.html#addImpl%28java.awt.Component, java.lang.Object, int%29), addPropertyChangeListener, [addPropertyChangeListener](../../java/awt/Container.html#addPropertyChangeListener%28java.lang.String, java.beans.PropertyChangeListener%29), applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, [findComponentAt](../../java/awt/Container.html#findComponentAt%28int, int%29), findComponentAt, getComponent, [getComponentAt](../../java/awt/Container.html#getComponentAt%28int, int%29), getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, [list](../../java/awt/Container.html#list%28java.io.PrintStream, int%29), [list](../../java/awt/Container.html#list%28java.io.PrintWriter, int%29), [locate](../../java/awt/Container.html#locate%28int, int%29), minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, [setComponentZOrder](../../java/awt/Container.html#setComponentZOrder%28java.awt.Component, int%29), setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusBackward, transferFocusDownCycle, validate, validateTree |
Methods inherited from class java.awt.Component |
---|
[action](../../java/awt/Component.html#action%28java.awt.Event, java.lang.Object%29), add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, [checkImage](../../java/awt/Component.html#checkImage%28java.awt.Image, java.awt.image.ImageObserver%29), [checkImage](../../java/awt/Component.html#checkImage%28java.awt.Image, int, int, java.awt.image.ImageObserver%29), [coalesceEvents](../../java/awt/Component.html#coalesceEvents%28java.awt.AWTEvent, java.awt.AWTEvent%29), contains, createImage, [createImage](../../java/awt/Component.html#createImage%28int, int%29), [createVolatileImage](../../java/awt/Component.html#createVolatileImage%28int, int%29), [createVolatileImage](../../java/awt/Component.html#createVolatileImage%28int, int, java.awt.ImageCapabilities%29), disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, [firePropertyChange](../../java/awt/Component.html#firePropertyChange%28java.lang.String, byte, byte%29), [firePropertyChange](../../java/awt/Component.html#firePropertyChange%28java.lang.String, double, double%29), [firePropertyChange](../../java/awt/Component.html#firePropertyChange%28java.lang.String, float, float%29), [firePropertyChange](../../java/awt/Component.html#firePropertyChange%28java.lang.String, long, long%29), [firePropertyChange](../../java/awt/Component.html#firePropertyChange%28java.lang.String, java.lang.Object, java.lang.Object%29), [firePropertyChange](../../java/awt/Component.html#firePropertyChange%28java.lang.String, short, short%29), getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, [gotFocus](../../java/awt/Component.html#gotFocus%28java.awt.Event, java.lang.Object%29), handleEvent, hasFocus, hide, [imageUpdate](../../java/awt/Component.html#imageUpdate%28java.awt.Image, int, int, int, int, int%29), [inside](../../java/awt/Component.html#inside%28int, int%29), isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, [keyDown](../../java/awt/Component.html#keyDown%28java.awt.Event, int%29), [keyUp](../../java/awt/Component.html#keyUp%28java.awt.Event, int%29), list, list, list, location, [lostFocus](../../java/awt/Component.html#lostFocus%28java.awt.Event, java.lang.Object%29), [mouseDown](../../java/awt/Component.html#mouseDown%28java.awt.Event, int, int%29), [mouseDrag](../../java/awt/Component.html#mouseDrag%28java.awt.Event, int, int%29), [mouseEnter](../../java/awt/Component.html#mouseEnter%28java.awt.Event, int, int%29), [mouseExit](../../java/awt/Component.html#mouseExit%28java.awt.Event, int, int%29), [mouseMove](../../java/awt/Component.html#mouseMove%28java.awt.Event, int, int%29), [mouseUp](../../java/awt/Component.html#mouseUp%28java.awt.Event, int, int%29), [move](../../java/awt/Component.html#move%28int, int%29), nextFocus, paintAll, postEvent, [prepareImage](../../java/awt/Component.html#prepareImage%28java.awt.Image, java.awt.image.ImageObserver%29), [prepareImage](../../java/awt/Component.html#prepareImage%28java.awt.Image, int, int, java.awt.image.ImageObserver%29), processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, [removePropertyChangeListener](../../java/awt/Component.html#removePropertyChangeListener%28java.lang.String, java.beans.PropertyChangeListener%29), repaint, [repaint](../../java/awt/Component.html#repaint%28int, int, int, int%29), repaint, resize, [resize](../../java/awt/Component.html#resize%28int, int%29), [setBounds](../../java/awt/Component.html#setBounds%28int, int, int, int%29), setBounds, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, [setLocation](../../java/awt/Component.html#setLocation%28int, int%29), setLocation, setName, setSize, [setSize](../../java/awt/Component.html#setSize%28int, int%29), show, show, size, toString, transferFocus, transferFocusUpCycle |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, [wait](../../java/lang/Object.html#wait%28long, int%29) |
Field Detail |
---|
W3C_LENGTH_UNITS
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 Boolean.TRUE
.
Since:
1.5
See Also:
HONOR_DISPLAY_PROPERTIES
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 Boolean.TRUE
.
Since:
1.5
See Also:
Constructor Detail |
---|
JEditorPane
public JEditorPane()
Creates a new JEditorPane
. The document model is set to null
.
JEditorPane
public JEditorPane(URL initialPage) throws IOException
Creates a JEditorPane
based on a specified URL for input.
Parameters:
initialPage
- the URL
Throws:
[IOException](../../java/io/IOException.html "class in java.io")
- if the URL is null
or cannot be accessed
JEditorPane
public JEditorPane(String url) throws IOException
Creates a JEditorPane
based on a string containing a URL specification.
Parameters:
url
- the URL
Throws:
[IOException](../../java/io/IOException.html "class in java.io")
- if the URL is null
or cannot be accessed
JEditorPane
public JEditorPane(String type, String text)
Creates a JEditorPane
that has been initialized to the given text. This is a convenience constructor that calls thesetContentType
and setText
methods.
Parameters:
type
- mime type of the given text
text
- the text to initialize with; may be null
Throws:
[NullPointerException](../../java/lang/NullPointerException.html "class in java.lang")
- if the type
parameter is null
Method Detail |
---|
addHyperlinkListener
public void addHyperlinkListener(HyperlinkListener listener)
Adds a hyperlink listener for notification of any changes, for example when a link is selected and entered.
Parameters:
listener
- the listener
removeHyperlinkListener
public void removeHyperlinkListener(HyperlinkListener listener)
Removes a hyperlink listener.
Parameters:
listener
- the listener
getHyperlinkListeners
public HyperlinkListener[] getHyperlinkListeners()
Returns an array of all the HyperLinkListener
s added to this JEditorPane with addHyperlinkListener().
Returns:
all of the HyperLinkListener
s added or an empty array if no listeners have been added
Since:
1.4
fireHyperlinkUpdate
public void fireHyperlinkUpdate(HyperlinkEvent e)
Notifies all listeners that have registered interest for notification on this event type. This is normally called by the currently installed 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.
Parameters:
e
- the event
See Also:
setPage
public void setPage(URL page) throws IOException
Sets the current URL being displayed. The content type of the pane is set, and if the editor kit for the pane is non-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 EditorKit
. If the Document
is of typeAbstractDocument
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 anIOException
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.
Parameters:
page
- the URL of the page
Throws:
[IOException](../../java/io/IOException.html "class in java.io")
- for a null
or invalid page specification, or exception from the stream being read
See Also:
read
public void read(InputStream in, Object desc) throws IOException
This method initializes from a stream. If the kit is set to be of type HTMLEditorKit
, and thedesc
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.
Parameters:
in
- the stream from which to read
desc
- an object describing the stream
Throws:
[IOException](../../java/io/IOException.html "class in java.io")
- as thrown by the stream being used to initialize
See Also:
[JTextComponent.read(java.io.Reader, java.lang.Object)](../../javax/swing/text/JTextComponent.html#read%28java.io.Reader, java.lang.Object%29), JTextComponent.setDocument(javax.swing.text.Document)
getStream
protected InputStream getStream(URL page) throws IOException
Fetches a stream for the given URL, which is about to be loaded by the 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.
Parameters:
page
- the URL of the page
Throws:
[IOException](../../java/io/IOException.html "class in java.io")
scrollToReference
public void scrollToReference(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). By default, this method only knows how to locate a reference in an HTMLDocument. The implementation calls thescrollRectToVisible
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.
Parameters:
reference
- the named location to scroll to
getPage
public URL getPage()
Gets the current URL being displayed. If a URL was not specified in the creation of the document, this will return null
, and relative URL's will not be resolved.
Returns:
the URL, or null
if none
setPage
public void setPage(String url) throws IOException
Sets the current URL being displayed.
Parameters:
url
- the URL for display
Throws:
[IOException](../../java/io/IOException.html "class in java.io")
- for a null
or invalid URL specification
getUIClassID
public String getUIClassID()
Gets the class ID for the UI.
Overrides:
[getUIClassID](../../javax/swing/JComponent.html#getUIClassID%28%29)
in class [JComponent](../../javax/swing/JComponent.html "class in javax.swing")
Returns:
the string "EditorPaneUI"
See Also:
JComponent.getUIClassID(), UIDefaults.getUI(javax.swing.JComponent)
createDefaultEditorKit
protected EditorKit createDefaultEditorKit()
Creates the default editor kit (PlainEditorKit
) for when the component is first created.
Returns:
the editor kit
getEditorKit
public EditorKit getEditorKit()
Fetches the currently installed kit for handling content.createDefaultEditorKit
is called to set up a default if necessary.
Returns:
the editor kit
getContentType
public final String getContentType()
Gets the type of content that this editor is currently set to deal with. This is defined to be the type associated with the currently installed EditorKit
.
Returns:
the content type, null
if no editor kit set
setContentType
public final void setContentType(String type)
Sets the type of content that this editor handles. This calls 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 fortext/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
.
Parameters:
type
- the non-null
mime type for the content editing support
Throws:
[NullPointerException](../../java/lang/NullPointerException.html "class in java.lang")
- if the type
parameter is null
See Also:
setEditorKit
public void setEditorKit(EditorKit kit)
Sets the currently installed kit for handling content. This is the bound property that establishes the content type of the editor. Any old kit is first deinstalled, then if kit is non-null
, the new kit is installed, and a default document created for it. A PropertyChange
event ("editorKit") is always fired whensetEditorKit
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.
Parameters:
kit
- the desired editor behavior
See Also:
getEditorKitForContentType
public EditorKit getEditorKitForContentType(String type)
Fetches the editor kit to use for the given type of content. This is called when a type is requested that doesn't match the currently installed type. If the component doesn't have an 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.
Parameters:
type
- the non-null content type
Returns:
the editor kit
setEditorKitForContentType
public void setEditorKitForContentType(String type, EditorKit k)
Directly sets the editor kit to use for the given type. A look-and-feel implementation might use this in conjunction with createEditorKitForContentType
to install handlers for content types with a look-and-feel bias.
Parameters:
type
- the non-null
content type
k
- the editor kit to be set
replaceSelection
public void replaceSelection(String content)
Replaces the currently selected content with new content represented by the given string. If there is no selection this amounts to an insert of the given text. If there is no replacement text (i.e. the content string is empty or 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.
Overrides:
[replaceSelection](../../javax/swing/text/JTextComponent.html#replaceSelection%28java.lang.String%29)
in class [JTextComponent](../../javax/swing/text/JTextComponent.html "class in javax.swing.text")
Parameters:
content
- the content to replace the selection with. This value can be null
createEditorKitForContentType
public static EditorKit createEditorKitForContentType(String type)
Creates a handler for the given type from the default registry of editor kits. The registry is created if necessary. If the registered class has not yet been loaded, an attempt is made to dynamically load the prototype of the kit for the given type. If the type was registered with a 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
instance is successfully located, it is cloned and the clone is returned.
Parameters:
type
- the content type
Returns:
the editor kit, or null
if there is nothing registered for the given type
registerEditorKitForContentType
public static void registerEditorKitForContentType(String type, String classname)
Establishes the default bindings of type
toclassname
. 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.
Parameters:
type
- the non-null
content type
classname
- the class to load later
registerEditorKitForContentType
public static void registerEditorKitForContentType(String type, String classname, ClassLoader loader)
Establishes the default bindings of type
toclassname
. 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.
Parameters:
type
- the non-null content type
classname
- the class to load later
loader
- the ClassLoader
to use to load the name
getEditorKitClassNameForContentType
public static String getEditorKitClassNameForContentType(String type)
Returns the currently registered EditorKit
class name for the type type
.
Parameters:
type
- the non-null
content type
Since:
1.3
getPreferredSize
public Dimension getPreferredSize()
Returns the preferred size for the 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.
Overrides:
[getPreferredSize](../../javax/swing/JComponent.html#getPreferredSize%28%29)
in class [JComponent](../../javax/swing/JComponent.html "class in javax.swing")
Returns:
a Dimension
containing the preferred size
See Also:
JComponent.setPreferredSize(java.awt.Dimension), ComponentUI
setText
public void setText(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. 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 currentEditorKit
. 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.
- Leaving the existing model in place means that the old view will be torn down, and a new view created, where replacing the document would avoid the tear down of the old view.
- Some formats (such as HTML) can install things into the document that can influence future contents. HTML can have style information embedded that would influence the next content installed unexpectedly.
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.
Overrides:
[setText](../../javax/swing/text/JTextComponent.html#setText%28java.lang.String%29)
in class [JTextComponent](../../javax/swing/text/JTextComponent.html "class in javax.swing.text")
Parameters:
t
- the new text to be set; if null
the old text will be deleted
See Also:
getText
public String getText()
Returns the text contained in this 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
.
Overrides:
[getText](../../javax/swing/text/JTextComponent.html#getText%28%29)
in class [JTextComponent](../../javax/swing/text/JTextComponent.html "class in javax.swing.text")
Returns:
the text
See Also:
getScrollableTracksViewportWidth
public boolean getScrollableTracksViewportWidth()
Returns true if a viewport should always force the width of this Scrollable
to match the width of the viewport.
Specified by:
[getScrollableTracksViewportWidth](../../javax/swing/Scrollable.html#getScrollableTracksViewportWidth%28%29)
in interface [Scrollable](../../javax/swing/Scrollable.html "interface in javax.swing")
Overrides:
[getScrollableTracksViewportWidth](../../javax/swing/text/JTextComponent.html#getScrollableTracksViewportWidth%28%29)
in class [JTextComponent](../../javax/swing/text/JTextComponent.html "class in javax.swing.text")
Returns:
true if a viewport should force the Scrollables width to match its own, false otherwise
getScrollableTracksViewportHeight
public boolean getScrollableTracksViewportHeight()
Returns true if a viewport should always force the height of this Scrollable
to match the height of the viewport.
Specified by:
[getScrollableTracksViewportHeight](../../javax/swing/Scrollable.html#getScrollableTracksViewportHeight%28%29)
in interface [Scrollable](../../javax/swing/Scrollable.html "interface in javax.swing")
Overrides:
[getScrollableTracksViewportHeight](../../javax/swing/text/JTextComponent.html#getScrollableTracksViewportHeight%28%29)
in class [JTextComponent](../../javax/swing/text/JTextComponent.html "class in javax.swing.text")
Returns:
true if a viewport should force theScrollable
's height to match its own, false otherwise
paramString
protected String paramString()
Returns a string representation of this 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
.
Overrides:
[paramString](../../javax/swing/text/JTextComponent.html#paramString%28%29)
in class [JTextComponent](../../javax/swing/text/JTextComponent.html "class in javax.swing.text")
Returns:
a string representation of this JEditorPane
getAccessibleContext
public AccessibleContext getAccessibleContext()
Gets the AccessibleContext associated with this JEditorPane. For editor panes, the AccessibleContext takes the form of an AccessibleJEditorPane. A new AccessibleJEditorPane instance is created if necessary.
Specified by:
[getAccessibleContext](../../javax/accessibility/Accessible.html#getAccessibleContext%28%29)
in interface [Accessible](../../javax/accessibility/Accessible.html "interface in javax.accessibility")
Overrides:
[getAccessibleContext](../../javax/swing/text/JTextComponent.html#getAccessibleContext%28%29)
in class [JTextComponent](../../javax/swing/text/JTextComponent.html "class in javax.swing.text")
Returns:
an AccessibleJEditorPane that serves as the AccessibleContext of this JEditorPane
Submit a bug or feature
For further API reference and developer documentation, see Java 2 SDK SE Developer Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 2004, 2010 Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.