- All Implemented Interfaces:
ImageObserver
,MenuContainer
,Serializable
,Appendable
,Accessible
,Scrollable
The method setSwingSafe(boolean)
can be used
to configure this component so that it can be modified from any thread,
not just the event dispatch thread. Allowing the component to be
modified from any thread is useful for applications with long running
times that change the contents of this component.
This class makes use of the static method
SwingUtilities.invokeLater(Runnable)
. One should avoid the
use of synchronized methods that call methods in this class when
those synchronized methods might be called from tasks waiting on
the AWT event dispatch queue, as there is a possibility of
deadlock: If for some class methods m1 and m2 are synchronized and
call one of the methods in this class, and m1 is called, a call to
SwingUtilities.invokeLater(Runnable)
may process other
entries on its event queue first, causing m2 to be called, but m2
will wait until m1 returns, which cannot occur until m2 returns.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionclass
SimpleJTextPane runtime-exception class.class
Saved attribute state for a SimpleJTextPane.Nested classes/interfaces inherited from class javax.swing.JEditorPane
JEditorPane.AccessibleJEditorPane, JEditorPane.AccessibleJEditorPaneHTML, JEditorPane.JEditorPaneAccessibleHypertextSupport
Nested classes/interfaces inherited from class javax.swing.text.JTextComponent
JTextComponent.AccessibleJTextComponent, JTextComponent.DropLocation, 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.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
-
Field Summary
Fields inherited from class javax.swing.JEditorPane
HONOR_DISPLAY_PROPERTIES, W3C_LENGTH_UNITS
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, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
Fields inherited from class java.awt.Component
accessibleContext, 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
ConstructorsConstructorDescriptionDefault Constructor.Constructor with a specific underlying document. -
Method Summary
Modifier and TypeMethodDescriptionappend
(char c) Append a character to the end of the component's document.append
(CharSequence csq) Append text to the end of the component's document.append
(CharSequence csq, int start, int end) Append text to the end of the component's document.void
appendString
(String str) Insert text at the end of the component's document.void
clear()
Remove all the text from a document.int
Get the number of characters in this text pane.Get the background color for subsequently inserted or appended text.Get the color of subsequently inserted or appended text.void
insertString
(int off, String str) Insert text into the component's document.boolean
isBold()
Determine if the font weight is appropriate for bold text.boolean
isItalic()
Determine if newly inserted text will be in italics.boolean
Test if this component is thread safe.void
Run a sequence of operations atomically.void
remove
(int start, int length) Remove text from the document the component displays.Save an attribute state so that it can be restored later.void
setBold
(boolean value) Set the font weight for subsequently inserted text to bold or normal.void
setDocument
(Document doc) void
setItalic
(boolean value) Set the font slant for subsequently inserted text.void
void
setSwingSafe
(boolean flag) Set whether or not this component is thread-safe.void
Set the background color for new text.void
Set the text color for new text.Methods inherited from class javax.swing.JTextPane
addStyle, createDefaultEditorKit, getCharacterAttributes, getInputAttributes, getLogicalStyle, getParagraphAttributes, getStyle, getStyledDocument, getStyledEditorKit, getUIClassID, insertComponent, insertIcon, paramString, removeStyle, replaceSelection, setCharacterAttributes, setEditorKit, setLogicalStyle, setParagraphAttributes
Methods inherited from class javax.swing.JEditorPane
addHyperlinkListener, createEditorKitForContentType, fireHyperlinkUpdate, getAccessibleContext, getContentType, getEditorKit, getEditorKitClassNameForContentType, getEditorKitForContentType, getHyperlinkListeners, getPage, getPreferredSize, getScrollableTracksViewportHeight, getScrollableTracksViewportWidth, getStream, getText, read, registerEditorKitForContentType, registerEditorKitForContentType, removeHyperlinkListener, scrollToReference, setContentType, setEditorKitForContentType, setPage, setPage, setText
Methods inherited from class javax.swing.text.JTextComponent
addCaretListener, addInputMethodListener, addKeymap, copy, cut, fireCaretUpdate, getActions, getCaret, getCaretColor, getCaretListeners, getCaretPosition, getDisabledTextColor, getDocument, getDragEnabled, getDropLocation, getDropMode, getFocusAccelerator, getHighlighter, getInputMethodRequests, getKeymap, getKeymap, getMargin, getNavigationFilter, getPreferredScrollableViewportSize, getPrintable, getScrollableBlockIncrement, getScrollableUnitIncrement, getSelectedText, getSelectedTextColor, getSelectionColor, getSelectionEnd, getSelectionStart, getText, getToolTipText, getUI, isEditable, loadKeymap, modelToView, modelToView2D, moveCaretPosition, paste, print, print, print, processInputMethodEvent, read, removeCaretListener, removeKeymap, removeNotify, restoreComposedText, saveComposedText, select, selectAll, setCaret, setCaretColor, setCaretPosition, setComponentOrientation, setDisabledTextColor, setDragEnabled, setDropMode, setEditable, setFocusAccelerator, setHighlighter, setKeymap, setMargin, setNavigationFilter, setSelectedTextColor, setSelectionColor, setSelectionEnd, setSelectionStart, setUI, updateUI, viewToModel, viewToModel2D, write
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, 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, hide, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingOrigin, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusDownCycle, validate, validateTree
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, 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, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, resize, resize, setBounds, setBounds, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setMixingCutoutShape, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
-
Constructor Details
-
SimpleJTextPane
public SimpleJTextPane()Default Constructor. -
SimpleJTextPane
Constructor with a specific underlying document.- Parameters:
doc
- the document for the component's model.
-
-
Method Details
-
saveAttributeState
Save an attribute state so that it can be restored later. The value returned may be used in a try-with-resources block so that the state will be automatically restored when the block exits for whatever reason.This method is thread safe.
- Returns:
- an object storing the attribute state
-
perform
Run a sequence of operations atomically. The argument c is a lambda express that will be called with its single argument set to this SimpleJTextPane. Because this method is synchronized, the operations the lambda expression performs will not be interleaved with synchronized calls from other threads. Alternatively, one can implement the consumer's accept method instead of using a lambda expression.This method is thread safe.
- Parameters:
c
- the consumer
-
isBold
public boolean isBold()Determine if the font weight is appropriate for bold text.This method is thread safe.
- Returns:
- true if newly inserted text will be bold; false otherwise
-
setBold
public void setBold(boolean value) Set the font weight for subsequently inserted text to bold or normal.This method is thread safe.
- Parameters:
value
- true if the font is bold; false if it's weight is normal.
-
isItalic
public boolean isItalic()Determine if newly inserted text will be in italics.This method is thread safe.
- Returns:
- true if newly inserted text will be italics; false otherwise
-
setItalic
public void setItalic(boolean value) Set the font slant for subsequently inserted text.This method is thread safe.
- Parameters:
value
- true if the font is italic; false if not.
-
getTextForeground
Get the color of subsequently inserted or appended text. The initial value is the foreground color of this pane when it is created (if not set, it inherits the color for this pane's parent).This method is thread safe.
- Returns:
- The color that will be used for new text.
-
setTextForeground
Set the text color for new text.This method is thread safe.
- Parameters:
fg
- the color for subsequently inserted or appended text.
-
getTextBackground
Get the background color for subsequently inserted or appended text. The initial value is the background color of this pane when it is created (if not set, it inherits the color for this pane's parent).This method is thread safe.
- Returns:
- The color that will be used for the background for new text.
-
setTextBackground
Set the background color for new text.This method is thread safe.
- Parameters:
bg
- the background color for subsequently inserted or appended text.
-
setSwingSafe
public void setSwingSafe(boolean flag) Set whether or not this component is thread-safe.- Parameters:
flag
- true if this component is thread safe; false if modifications to this component must be made from the event dispatch thread.
-
isSwingSafe
public boolean isSwingSafe()Test if this component is thread safe.This method is thread safe.
- Returns:
- true if this component is thread-safe; false if changes must be made from the event dispatch thread
-
setDocument
- Overrides:
setDocument
in classJTextPane
-
setStyledDocument
- Overrides:
setStyledDocument
in classJTextPane
-
insertString
Insert text into the component's document.This method is thread safe if when
isSwingSafe()
returns true.- Parameters:
off
- the offset into the document at which the new text should be insertedstr
- the text to insert.- Throws:
BadLocationException
- attempted to insert text at a non-existent location
-
getLength
public int getLength()Get the number of characters in this text pane.This method is thread safe if when
isSwingSafe()
returns true.- Returns:
- the number of characters
-
appendString
Insert text at the end of the component's document.This method is thread safe.
- Parameters:
str
- the text to insert.
-
append
Append a character to the end of the component's document.This method is thread safe.
- Specified by:
append
in interfaceAppendable
- Parameters:
c
- the character to append- Returns:
- the current object cast as an Appendable
-
append
Append text to the end of the component's document.This method is thread safe.
- Specified by:
append
in interfaceAppendable
- Parameters:
csq
- the text to append- Returns:
- the current object cast as an Appendable
-
append
Append text to the end of the component's document.This method is thread safe.
- Specified by:
append
in interfaceAppendable
- Parameters:
csq
- a CharSequence containing the text to appendstart
- the offset to the start of the text in csqend
- The position just past the end of the text to in csq to append- Returns:
- the current object cast as an Appendable
-
remove
Remove text from the document the component displays.This method is thread safe if when
isSwingSafe()
returns true.- Parameters:
start
- the starting offset into the document for the text to be removed..length
- the length of the text to be removed.- Throws:
BadLocationException
- the arguments cover a range of text that is not part of the document.
-
clear
public void clear()Remove all the text from a document.This method is thread safe.
-