javax.swing.text

Class JTextComponent

Implemented Interfaces:
Accessible, ImageObserver, MenuContainer, Scrollable, Serializable
Known Direct Subclasses:
JEditorPane, JTextArea, JTextField

public abstract class JTextComponent
extends JComponent
implements Scrollable, Accessible

See Also:
Serialized Form

Nested Class Summary

class
JTextComponent.AccessibleJTextComponent
AccessibleJTextComponent implements accessibility hooks for JTextComponent.
static class
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
DEFAULT_KEYMAP
static String
FOCUS_ACCELERATOR_KEY

Fields inherited from class javax.swing.JComponent

TOOL_TIP_TEXT_KEY, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW, accessibleContext, listenerList, ui

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

JTextComponent()
Creates a new JTextComponent instance.

Method Summary

void
addCaretListener(CaretListener listener)
Adds a CaretListener object to this text component.
void
addInputMethodListener(InputMethodListener listener)
Adds an InputListener object to this text component.
static Keymap
addKeymap(String n, Keymap parent)
Create a new Keymap with a specific name and parent, and add the new Keymap to the global keymap table.
void
copy()
void
cut()
protected void
fireCaretUpdate(CaretEvent event)
Notifies all registered CaretListener objects that the caret was updated.
AccessibleContext
getAccessibleContext()
Get the AccessibleContext of this object.
Action[]
getActions()
Returns the set of available Actions this component's associated editor can run.
Caret
getCaret()
The Caret object used in this text component.
Color
getCaretColor()
CaretListener[]
getCaretListeners()
Returns all added CaretListener objects.
int
getCaretPosition()
Retrisves the current caret position.
Color
getDisabledTextColor()
Document
getDocument()
boolean
getDragEnabled()
char
getFocusAccelerator()
Highlighter
getHighlighter()
InputMethodListener[]
getInputMethodListeners()
Returns all added InputMethodListener objects.
Keymap
getKeymap()
Get the current Keymap of this component.
static Keymap
getKeymap(String n)
Get a Keymap from the global keymap table, by name.
Insets
getMargin()
NavigationFilter
getNavigationFilter()
Dimension
getPreferredScrollableViewportSize()
int
getScrollableBlockIncrement(Rectangle visible, int orientation, int direction)
Return the preferred scrolling amount (in pixels) for the given scrolling direction and orientation when scrolling in large amounts (pages).
boolean
getScrollableTracksViewportHeight()
Return true if the height of the scrollable is always equal to the view, where it is displayed, height.In such case, the vertical scrolling should not be performed.
boolean
getScrollableTracksViewportWidth()
Return true if the width of the scrollable is always equal to the view, where it is displayed, width (for instance, the text area with the word wrap).
int
getScrollableUnitIncrement(Rectangle visible, int orientation, int direction)
Return the preferred scrolling amount (in pixels) for the given scrolling direction and orientation when scrolling in small amounts like table lines.
String
getSelectedText()
Retrieves the currently selected text in this text document.
Color
getSelectedTextColor()
Color
getSelectionColor()
int
getSelectionEnd()
Returns the end postion of the currently selected text.
int
getSelectionStart()
Returns the start postion of the currently selected text.
String
getText()
Retrieves the current text in this text document.
String
getText(int offset, int length)
Retrieves a part of the current text in this document.
String
getToolTipText(MouseEvent ev)
Returns the tooltip text for this text component for the given mouse event.
TextUI
getUI()
This method returns the label's UI delegate.
String
getUIClassID()
Returns a string that specifies the name of the Look and Feel class that renders this component.
boolean
isEditable()
Checks whether this text component it editable.
static void
loadKeymap(Keymap map, JTextComponent.KeyBinding[] bindings, Action[] actions)
Resolves a set of bindings against a set of actions and inserts the results into a Keymap.
Rectangle
modelToView(int position)
void
moveCaretPosition(int position)
Moves the caret to a given position.
protected String
paramString()
Returns a string representation of this JTextComponent.
void
paste()
void
read(Reader input, Object streamDescription)
Read and set the content this component.
void
removeCaretListener(CaretListener listener)
Removed a CaretListener object from this text component.
void
removeInputMethodListener(InputMethodListener listener)
Removes an InputListener object from this text component.
static Keymap
removeKeymap(String n)
Remove a Keymap from the global Keymap table, by name.
void
replaceSelection(String content)
void
select(int start, int end)
Selects a part of the content of the text component.
void
selectAll()
Selects the whole content of the text component.
void
setCaret(Caret newCaret)
Sets a new Caret for this text component.
void
setCaretColor(Color newColor)
void
setCaretPosition(int position)
Sets the caret to a new position.
void
setDisabledTextColor(Color newColor)
void
setDocument(Document newDoc)
void
setDragEnabled(boolean enabled)
void
setEditable(boolean newValue)
Enables/disabled this text component's editability.
void
setFocusAccelerator(char newKey)
void
setHighlighter(Highlighter newHighlighter)
void
setKeymap(Keymap k)
Set the current Keymap of this component, installing appropriate KeymapWrapper and KeymapActionMap objects in the InputMap and ActionMap parent chains, respectively, and fire a property change event with name "keymap".
void
setMargin(Insets m)
void
setNavigationFilter(NavigationFilter filter)
void
setSelectedTextColor(Color newColor)
void
setSelectionColor(Color newColor)
void
setSelectionEnd(int end)
Selects the text from the selection start postion to the given position.
void
setSelectionStart(int start)
Selects the text from the given postion to the selection end position.
void
setText(String text)
void
setUI(TextUI newUI)
This method sets the label's UI delegate.
void
updateUI()
This method resets the label's UI delegate to the default UI for the current look and feel.
int
viewToModel(Point pt)
void
write(Writer output)
Write the content of this component to the given stream.

Methods inherited from class javax.swing.JComponent

addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, extends EventListener> T[] getListeners, firePropertyChange, firePropertyChange, fireVetoableChange, getAccessibleContext, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getUIClassID, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, paramString, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, 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, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setNextFocusableComponent, setOpaque, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update, updateUI

Methods inherited from class java.awt.Container

add, add, add, add, add, addContainerListener, addImpl, addNotify, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, extends EventListener> T[] getListeners, findComponentAt, findComponentAt, getAlignmentX, getAlignmentY, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponentZOrder, getComponents, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getInsets, getLayout, getMaximumSize, getMinimumSize, getPreferredSize, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paint, paintComponents, paramString, preferredSize, print, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, removeNotify, setComponentZOrder, setFocusCycleRoot, setFocusTraversalKeys, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setFont, setLayout, transferFocusDownCycle, update, validate, validateTree

Methods inherited from class java.awt.Component

action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, addNotify, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, deliverEvent, disable, disableEvents, dispatchEvent, doLayout, enable, enable, enableEvents, enableInputMethods, extends EventListener> T[] getListeners, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAccessibleContext, getAlignmentX, getAlignmentY, getBackground, getBounds, getBounds, getColorModel, getComponentAt, getComponentAt, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeys, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphics, getGraphicsConfiguration, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocation, getLocationOnScreen, getMaximumSize, getMinimumSize, getMouseListeners, getMouseMotionListeners, getMouseWheelListeners, getName, getParent, getPeer, getPreferredSize, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getToolkit, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, invalidate, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusCycleRoot, isFocusOwner, isFocusTraversable, isFocusable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, layout, list, list, list, list, list, locate, location, lostFocus, minimumSize, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paint, paintAll, paramString, postEvent, preferredSize, prepareImage, prepareImage, print, printAll, processComponentEvent, processEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, reshape, resize, resize, setBackground, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFocusTraversalKeys, setFocusTraversalKeysEnabled, setFocusable, setFont, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setMaximumSize, setMinimumSize, setName, setPreferredSize, setSize, setSize, setVisible, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle, update, validate

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Field Details

DEFAULT_KEYMAP

public static final String DEFAULT_KEYMAP
Field Value:
"default"

FOCUS_ACCELERATOR_KEY

public static final String FOCUS_ACCELERATOR_KEY
Field Value:
"focusAcceleratorKey"

Constructor Details

JTextComponent

public JTextComponent()
Creates a new JTextComponent instance.

Method Details

addCaretListener

public void addCaretListener(CaretListener listener)
Adds a CaretListener object to this text component.
Parameters:
listener - the listener to add

addInputMethodListener

public void addInputMethodListener(InputMethodListener listener)
Adds an InputListener object to this text component.
Overrides:
addInputMethodListener in interface Component
Parameters:
listener - the listener to add

addKeymap

public static Keymap addKeymap(String n,
                               Keymap parent)
Create a new Keymap with a specific name and parent, and add the new Keymap to the global keymap table. The name may be null, in which case the new Keymap will not be added to the global Keymap table. The parent may also be null, which is harmless.
Parameters:
n - The name of the new Keymap, or null
parent - The parent of the new Keymap, or null
Returns:
The newly created Keymap

copy

public void copy()

cut

public void cut()

fireCaretUpdate

protected void fireCaretUpdate(CaretEvent event)
Notifies all registered CaretListener objects that the caret was updated.
Parameters:
event - the event to send

getAccessibleContext

public AccessibleContext getAccessibleContext()
Get the AccessibleContext of this object.
Specified by:
getAccessibleContext in interface Accessible
Overrides:
getAccessibleContext in interface JComponent
Returns:
an AccessibleContext object

getActions

public Action[] getActions()
Returns the set of available Actions this component's associated editor can run. Equivalent to calling getUI().getEditorKit().getActions(). This set of Actions is a reasonable value to provide as a parameter to loadKeymap(Keymap,JTextComponent.KeyBinding[],Action[]), when resolving a set of JTextComponent.KeyBinding objects against this component.
Returns:
The set of available Actions on this component's EditorKit

getCaret

public Caret getCaret()
The Caret object used in this text component.
Returns:
the caret object

getCaretColor

public Color getCaretColor()

getCaretListeners

public CaretListener[] getCaretListeners()
Returns all added CaretListener objects.
Returns:
an array of listeners

getCaretPosition

public int getCaretPosition()
Retrisves the current caret position.
Returns:
the current position

getDisabledTextColor

public Color getDisabledTextColor()

getDocument

public Document getDocument()

getDragEnabled

public boolean getDragEnabled()

getFocusAccelerator

public char getFocusAccelerator()

getHighlighter

public Highlighter getHighlighter()

getInputMethodListeners

public InputMethodListener[] getInputMethodListeners()
Returns all added InputMethodListener objects.
Overrides:
getInputMethodListeners in interface Component
Returns:
an array of listeners

getKeymap

public Keymap getKeymap()
Get the current Keymap of this component.
Returns:
The component's current Keymap

getKeymap

public static Keymap getKeymap(String n)
Get a Keymap from the global keymap table, by name.
Parameters:
n - The name of the Keymap to look up
Returns:
A Keymap associated with the provided name, or null if no such Keymap exists

getMargin

public Insets getMargin()

getNavigationFilter

public NavigationFilter getNavigationFilter()
Since:
1.4

getPreferredScrollableViewportSize

public Dimension getPreferredScrollableViewportSize()
Specified by:
getPreferredScrollableViewportSize in interface Scrollable

getScrollableBlockIncrement

public int getScrollableBlockIncrement(Rectangle visible,
                                       int orientation,
                                       int direction)
Return the preferred scrolling amount (in pixels) for the given scrolling direction and orientation when scrolling in large amounts (pages). <