Package edu.cnu.mdi.component
Class TextPaneScrollPane
java.lang.Object
java.awt.Component
java.awt.Container
javax.swing.JComponent
javax.swing.JScrollPane
edu.cnu.mdi.component.TextPaneScrollPane
- All Implemented Interfaces:
ImageObserver,MenuContainer,Serializable,Accessible,ScrollPaneConstants
- Direct Known Subclasses:
FeedbackPane,JsonRawPane,LogPane
A
JScrollPane that owns a non-editable JTextPane using a
StyledDocument and a collection of convenience styles for terminal /
status-like output.
This class is also written to behave well under the Nimbus look & feel:
the scroll pane and its viewport are opaque and painted with the configured
background color, while the embedded JTextPane is non-opaque with a
transparent background. This avoids Nimbus painting only behind the text
while leaving the rest of the area white.
Typical usage:
TextPaneScrollPane pane = new TextPaneScrollPane("Feedback", Color.BLACK);
pane.append("Hello world\n", TextPaneScrollPane.CYAN_TERMINAL);
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class javax.swing.JScrollPane
JScrollPane.AccessibleJScrollPane, JScrollPane.ScrollBarNested classes/interfaces inherited from class javax.swing.JComponent
JComponent.AccessibleJComponentNested classes/interfaces inherited from class java.awt.Container
Container.AccessibleAWTContainerNested classes/interfaces inherited from class java.awt.Component
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final SimpleAttributeSetSmall black plain sans-serif.static final SimpleAttributeSetBlack plain sans-serif.static final SimpleAttributeSetSmall blue monospaced style on a light blue background.static final SimpleAttributeSetSmall blue bold sans-serif on a light background.static final SimpleAttributeSetBlue bold sans-serif on a light background.static final SimpleAttributeSetBlue plain sans-serif.static final SimpleAttributeSetBlue monospaced terminal style.static final SimpleAttributeSetCyan monospaced terminal style on a transparent background.protected SimpleAttributeSetDefault style used when no explicit style is supplied.protected StyledDocumentThe styled document backing the text pane.static final SimpleAttributeSetSmall green plain sans-serif on a yellow background.static final SimpleAttributeSetDark green bold sans-serif on a wheat background.static final SimpleAttributeSetDark green plain sans-serif on a wheat background.static final SimpleAttributeSetGreen monospaced terminal style on a transparent background.static final SimpleAttributeSetSmall red plain sans-serif.static final SimpleAttributeSetRed plain sans-serif.static final SimpleAttributeSetRed monospaced terminal style on a transparent background.protected JTextPaneThe non-editable text pane hosted by this scroll pane.static final SimpleAttributeSetYellow monospaced terminal style on a transparent background.Fields inherited from class javax.swing.JScrollPane
columnHeader, horizontalScrollBar, horizontalScrollBarPolicy, lowerLeft, lowerRight, rowHeader, upperLeft, upperRight, verticalScrollBar, verticalScrollBarPolicy, viewportFields inherited from class javax.swing.JComponent
listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOWFields inherited from class java.awt.Component
accessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENTFields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTHFields inherited from interface javax.swing.ScrollPaneConstants
COLUMN_HEADER, HORIZONTAL_SCROLLBAR, HORIZONTAL_SCROLLBAR_ALWAYS, HORIZONTAL_SCROLLBAR_AS_NEEDED, HORIZONTAL_SCROLLBAR_NEVER, HORIZONTAL_SCROLLBAR_POLICY, LOWER_LEADING_CORNER, LOWER_LEFT_CORNER, LOWER_RIGHT_CORNER, LOWER_TRAILING_CORNER, ROW_HEADER, UPPER_LEADING_CORNER, UPPER_LEFT_CORNER, UPPER_RIGHT_CORNER, UPPER_TRAILING_CORNER, VERTICAL_SCROLLBAR, VERTICAL_SCROLLBAR_ALWAYS, VERTICAL_SCROLLBAR_AS_NEEDED, VERTICAL_SCROLLBAR_NEVER, VERTICAL_SCROLLBAR_POLICY, VIEWPORT -
Constructor Summary
ConstructorsConstructorDescriptionCreates a newTextPaneScrollPanewith a white background and no border label.TextPaneScrollPane(Color bgColor) Creates a newTextPaneScrollPanewith no border label.TextPaneScrollPane(String label, Color bgColor) Creates a newTextPaneScrollPane, optionally with a labeled border. -
Method Summary
Modifier and TypeMethodDescriptionvoidAppends plain text using the currentdefaultStyle.voidappend(String text, AttributeSet style) Appends text using a specific style.voidappend(String text, AttributeSet style, boolean writeTime) Appends text using a specific style and optional timestamp.voidclear()Clears all text and resets the caret position to the beginning.static SimpleAttributeSetcreateStyle(Color fg, Color bg, String fontFamily, int fontSize, boolean italic, boolean bold) Creates a style with the given foreground and background colors and default spacing.static SimpleAttributeSetcreateStyle(Color fg, Color bg, String fontFamily, int fontSize, boolean italic, boolean bold, boolean underline, int spaceAbove, int spaceBelow) Creates a fully specified style.static SimpleAttributeSetcreateStyle(Color fg, String fontFamily, int fontSize, boolean italic, boolean bold) Creates a style with the given characteristics and a default white background and spacing.Returns the current default style used byappend(String).voidrefresh()Forces a repaint of the underlying text pane.voidsetDefaultStyle(SimpleAttributeSet defaultStyle) Sets the default style used byappend(String).Methods inherited from class javax.swing.JScrollPane
createHorizontalScrollBar, createVerticalScrollBar, createViewport, getAccessibleContext, getColumnHeader, getCorner, getHorizontalScrollBar, getHorizontalScrollBarPolicy, getRowHeader, getUI, getUIClassID, getVerticalScrollBar, getVerticalScrollBarPolicy, getViewport, getViewportBorder, getViewportBorderBounds, isValidateRoot, isWheelScrollingEnabled, paramString, setColumnHeader, setColumnHeaderView, setComponentOrientation, setCorner, setHorizontalScrollBar, setHorizontalScrollBarPolicy, setLayout, setRowHeader, setRowHeaderView, setUI, setVerticalScrollBar, setVerticalScrollBarPolicy, setViewport, setViewportBorder, setViewportView, setWheelScrollingEnabled, updateUIMethods 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, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, hide, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingOrigin, isPaintingTile, isRequestFocusEnabled, paint, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, 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, updateMethods 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, transferFocusDownCycle, validate, validateTreeMethods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, 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, getInputMethodRequests, 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, processInputMethodEvent, 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
-
Field Details
-
BLUE_M_10_B
Small blue monospaced style on a light blue background. -
RED_TERMINAL
Red monospaced terminal style on a transparent background. -
YELLOW_TERMINAL
Yellow monospaced terminal style on a transparent background. -
CYAN_TERMINAL
Cyan monospaced terminal style on a transparent background. -
BLUE_TERMINAL
Blue monospaced terminal style. -
GREEN_TERMINAL
Green monospaced terminal style on a transparent background. -
BLUE_SS_12_B
Blue bold sans-serif on a light background. -
BLUE_SS_12_P
Blue plain sans-serif. -
GREEN_SS_12_B
Dark green bold sans-serif on a wheat background. -
RED_SS_12_P
Red plain sans-serif. -
GREEN_SS_12_P
Dark green plain sans-serif on a wheat background. -
BLACK_SS_12_P
Black plain sans-serif. -
BLUE_SS_10_B
Small blue bold sans-serif on a light background. -
RED_SS_10_P
Small red plain sans-serif. -
GREEN_SS_10_P
Small green plain sans-serif on a yellow background. -
BLACK_SS_10_P
Small black plain sans-serif. -
textPane
The non-editable text pane hosted by this scroll pane. -
document
The styled document backing the text pane. -
defaultStyle
Default style used when no explicit style is supplied.
-
-
Constructor Details
-
TextPaneScrollPane
public TextPaneScrollPane()Creates a newTextPaneScrollPanewith a white background and no border label. -
TextPaneScrollPane
Creates a newTextPaneScrollPanewith no border label.- Parameters:
bgColor- the background color to use; ifnull, white is used
-
TextPaneScrollPane
Creates a newTextPaneScrollPane, optionally with a labeled border.- Parameters:
label- optional border label; ifnull, no labeled border is installedbgColor- the background color to use; ifnull, white is used
-
-
Method Details
-
createStyle
public static SimpleAttributeSet createStyle(Color fg, String fontFamily, int fontSize, boolean italic, boolean bold) Creates a style with the given characteristics and a default white background and spacing.- Parameters:
fg- the foreground colorfontFamily- the font family to usefontSize- the font size to useitalic-truefor italic textbold-truefor bold text- Returns:
- the created style
-
createStyle
public static SimpleAttributeSet createStyle(Color fg, Color bg, String fontFamily, int fontSize, boolean italic, boolean bold) Creates a style with the given foreground and background colors and default spacing.- Parameters:
fg- the foreground colorbg- the background colorfontFamily- the font family to usefontSize- the font size to useitalic-truefor italic textbold-truefor bold text- Returns:
- the created style
-
createStyle
public static SimpleAttributeSet createStyle(Color fg, Color bg, String fontFamily, int fontSize, boolean italic, boolean bold, boolean underline, int spaceAbove, int spaceBelow) Creates a fully specified style.- Parameters:
fg- the foreground colorbg- the background colorfontFamily- the font family to usefontSize- the font size to useitalic-truefor italic textbold-truefor bold textunderline-trueto underlinespaceAbove- additional space above the paragraph, in pointsspaceBelow- additional space below the paragraph, in points- Returns:
- the created style
-
refresh
public void refresh()Forces a repaint of the underlying text pane. -
append
Appends plain text using the currentdefaultStyle.- Parameters:
text- the text to append;nullis ignored
-
append
Appends text using a specific style.- Parameters:
text- the text to append;nullis ignoredstyle- the style to use (for exampleCYAN_TERMINAL)
-
append
Appends text using a specific style and optional timestamp.- Parameters:
text- the text to append;nullis ignoredstyle- the style to usewriteTime-trueto prepend a timestamp usingBLUE_M_10_B
-
clear
public void clear()Clears all text and resets the caret position to the beginning. -
getDefaultStyle
Returns the current default style used byappend(String).- Returns:
- the default style
-
setDefaultStyle
Sets the default style used byappend(String).- Parameters:
defaultStyle- the new default style
-