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)
boolean
getScrollableTracksViewportHeight()
boolean
getScrollableTracksViewportWidth()
int
getScrollableUnitIncrement(Rectangle visible, int orientation, int direction)
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, getMousePosition, 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, getMousePosition, 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)
Specified by:
getScrollableBlockIncrement in interface Scrollable

getScrollableTracksViewportHeight

public boolean getScrollableTracksViewportHeight()
Specified by:
getScrollableTracksViewportHeight in interface Scrollable

getScrollableTracksViewportWidth

public boolean getScrollableTracksViewportWidth()
Specified by:
getScrollableTracksViewportWidth in interface Scrollable

getScrollableUnitIncrement

public int getScrollableUnitIncrement(Rectangle visible,
                                      int orientation,
                                      int direction)
Specified by:
getScrollableUnitIncrement in interface Scrollable

getSelectedText

public String getSelectedText()
Retrieves the currently selected text in this text document.
Returns:
the selected text
Throws:
NullPointerException - if the underlaying document is null

getSelectedTextColor

public Color getSelectedTextColor()

getSelectionColor

public Color getSelectionColor()

getSelectionEnd

public int getSelectionEnd()
Returns the end postion of the currently selected text.
Returns:
the end postion

getSelectionStart

public int getSelectionStart()
Returns the start postion of the currently selected text.
Returns:
the start postion

getText

public String getText()
Retrieves the current text in this text document.
Returns:
the text
Throws:
NullPointerException - if the underlaying document is null

getText

public String getText(int offset,
                      int length)
            throws BadLocationException
Retrieves a part of the current text in this document.
Parameters:
offset - the postion of the first character
length - the length of the text to retrieve
Returns:
the text
Throws:
BadLocationException - if arguments do not hold pre-conditions

getToolTipText

public String getToolTipText(MouseEvent ev)
Returns the tooltip text for this text component for the given mouse event. This forwards the call to TextUI.getToolTipText(JTextComponent,Point).
Overrides:
getToolTipText in interface JComponent
Parameters:
ev - the mouse event
Returns:
the tooltip text for this text component for the given mouse event

getUI

public TextUI getUI()
This method returns the label's UI delegate.
Returns:
The label's UI delegate.

getUIClassID

public String getUIClassID()
Returns a string that specifies the name of the Look and Feel class that renders this component.
Overrides:
getUIClassID in interface JComponent
Returns:
the string "TextComponentUI"

isEditable

public boolean isEditable()
Checks whether this text component it editable.
Returns:
true if editable, false otherwise

loadKeymap

public 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. Specifically, for each provided binding b, if there exists a provided action a such that a.getValue(Action.NAME) == b.ActionName then an entry is added to the Keymap mapping b to a.
Parameters:
map - The Keymap to add new mappings to
bindings - The set of bindings to add to the Keymap
actions - The set of actions to resolve binding names against

modelToView

public Rectangle modelToView(int position)
            throws BadLocationException

moveCaretPosition

public void moveCaretPosition(int position)
Moves the caret to a given position. This selects the text between the old and the new position of the caret.

paramString

protected String paramString()
Returns a string representation of this JTextComponent.
Overrides:
paramString in interface JComponent

paste

public void paste()

read

public void read(Reader input,
                 Object streamDescription)
            throws IOException
Read and set the content this component. If not overridden, the method reads the component content as a plain text. The second parameter of this method describes the input stream. It can be String, URL, File and so on. If not null, this object is added to the properties of the associated document under the key Document.StreamDescriptionProperty.
Parameters:
input - an input stream to read from.
streamDescription - an object, describing the stream.
Throws:
IOException - if the reader throws it.

removeCaretListener

public void removeCaretListener(CaretListener listener)
Removed a CaretListener object from this text component.
Parameters:
listener - the listener to remove

removeInputMethodListener

public void removeInputMethodListener(InputMethodListener listener)
Removes an InputListener object from this text component.
Overrides:
removeInputMethodListener in interface Component
Parameters:
listener - the listener to remove

removeKeymap

public static Keymap removeKeymap(String n)
Remove a Keymap from the global Keymap table, by name.
Parameters:
n - The name of the Keymap to remove
Returns:
The keymap removed from the global table

replaceSelection

public void replaceSelection(String content)

select

public void select(int start,
                   int end)
Selects a part of the content of the text component.
Parameters:
start - the start position of the selected text
end - the end position of the selected text

selectAll

public void selectAll()
Selects the whole content of the text component.

setCaret

public void setCaret(Caret newCaret)
Sets a new Caret for this text component.
Parameters:
newCaret - the new Caret to set

setCaretColor

public void setCaretColor(Color newColor)

setCaretPosition

public void setCaretPosition(int position)
Sets the caret to a new position.
Parameters:
position - the new position

setDisabledTextColor

public void setDisabledTextColor(Color newColor)

setDocument

public void setDocument(Document newDoc)

setDragEnabled

public void setDragEnabled(boolean enabled)

setEditable

public void setEditable(boolean newValue)
Enables/disabled this text component's editability.
Parameters:
newValue - true to make it editable, false otherwise.

setFocusAccelerator

public void setFocusAccelerator(char newKey)

setHighlighter

public void setHighlighter(Highlighter newHighlighter)

setKeymap

public 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".

setMargin

public void setMargin(Insets m)

setNavigationFilter

public void setNavigationFilter(NavigationFilter filter)
Since:
1.4

setSelectedTextColor

public void setSelectedTextColor(Color newColor)

setSelectionColor

public void setSelectionColor(Color newColor)

setSelectionEnd

public void setSelectionEnd(int end)
Selects the text from the selection start postion to the given position.
Parameters:
end - the end positon of the selected text.

setSelectionStart

public void setSelectionStart(int start)
Selects the text from the given postion to the selection end position.
Parameters:
start - the start positon of the selected text.

setText

public void setText(String text)

setUI

public void setUI(TextUI newUI)
This method sets the label's UI delegate.
Parameters:
newUI - The label's UI delegate.

updateUI

public void updateUI()
This method resets the label's UI delegate to the default UI for the current look and feel.
Overrides:
updateUI in interface JComponent

viewToModel

public int viewToModel(Point pt)

write

public void write(Writer output)
            throws IOException
Write the content of this component to the given stream. If not overridden, the method writes the component content as a plain text.
Parameters:
output - the writer to write into.
Throws:
IOException - if the writer throws it.

JTextComponent.java -- Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc. This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version.