javax.swing.text

Class JTextComponent

public abstract class JTextComponent extends JComponent implements Scrollable, Accessible

Nested Class Summary
classJTextComponent.AccessibleJTextComponent
AccessibleJTextComponent implements accessibility hooks for JTextComponent.
static classJTextComponent.KeyBinding
Field Summary
static StringDEFAULT_KEYMAP
static StringFOCUS_ACCELERATOR_KEY
Constructor Summary
JTextComponent()
Creates a new JTextComponent instance.
Method Summary
voidaddCaretListener(CaretListener listener)
Adds a CaretListener object to this text component.
voidaddInputMethodListener(InputMethodListener listener)
Adds an InputListener object to this text component.
static KeymapaddKeymap(String n, Keymap parent)
Create a new Keymap with a specific name and parent, and add the new Keymap to the global keymap table.
voidcopy()
voidcut()
protected voidfireCaretUpdate(CaretEvent event)
Notifies all registered CaretListener objects that the caret was updated.
AccessibleContextgetAccessibleContext()
Get the AccessibleContext of this object.
Action[]getActions()
Returns the set of available Actions this component's associated editor can run.
CaretgetCaret()
The Caret object used in this text component.
ColorgetCaretColor()
CaretListener[]getCaretListeners()
Returns all added CaretListener objects.
intgetCaretPosition()
Retrisves the current caret position.
ColorgetDisabledTextColor()
DocumentgetDocument()
booleangetDragEnabled()
chargetFocusAccelerator()
HighlightergetHighlighter()
InputMethodListener[]getInputMethodListeners()
Returns all added InputMethodListener objects.
static KeymapgetKeymap(String n)
Get a Keymap from the global keymap table, by name.
KeymapgetKeymap()
Get the current Keymap of this component.
InsetsgetMargin()
NavigationFiltergetNavigationFilter()
DimensiongetPreferredScrollableViewportSize()
intgetScrollableBlockIncrement(Rectangle visible, int orientation, int direction)
booleangetScrollableTracksViewportHeight()
booleangetScrollableTracksViewportWidth()
intgetScrollableUnitIncrement(Rectangle visible, int orientation, int direction)
StringgetSelectedText()
Retrieves the currently selected text in this text document.
ColorgetSelectedTextColor()
ColorgetSelectionColor()
intgetSelectionEnd()
Returns the end postion of the currently selected text.
intgetSelectionStart()
Returns the start postion of the currently selected text.
StringgetText()
Retrieves the current text in this text document.
StringgetText(int offset, int length)
Retrieves a part of the current text in this document.
StringgetToolTipText(MouseEvent ev)
Returns the tooltip text for this text component for the given mouse event.
TextUIgetUI()
This method returns the label's UI delegate.
StringgetUIClassID()
Returns a string that specifies the name of the Look and Feel class that renders this component.
booleanisEditable()
Checks whether this text component it editable.
static voidloadKeymap(Keymap map, JTextComponent.KeyBinding[] bindings, Action[] actions)
Resolves a set of bindings against a set of actions and inserts the results into a {@link Keymap}.
RectanglemodelToView(int position)
voidmoveCaretPosition(int position)
Moves the caret to a given position.
protected StringparamString()
Returns a string representation of this JTextComponent.
voidpaste()
voidread(Reader input, Object streamDescription)
Read and set the content this component.
voidremoveCaretListener(CaretListener listener)
Removed a CaretListener object from this text component.
voidremoveInputMethodListener(InputMethodListener listener)
Removes an InputListener object from this text component.
static KeymapremoveKeymap(String n)
Remove a Keymap from the global Keymap table, by name.
voidreplaceSelection(String content)
voidselect(int start, int end)
Selects a part of the content of the text component.
voidselectAll()
Selects the whole content of the text component.
voidsetCaret(Caret newCaret)
Sets a new Caret for this text component.
voidsetCaretColor(Color newColor)
voidsetCaretPosition(int position)
Sets the caret to a new position.
voidsetDisabledTextColor(Color newColor)
voidsetDocument(Document newDoc)
voidsetDragEnabled(boolean enabled)
voidsetEditable(boolean newValue)
Enables/disabled this text component's editability.
voidsetFocusAccelerator(char newKey)
voidsetHighlighter(Highlighter newHighlighter)
voidsetKeymap(Keymap k)
Set the current Keymap of this component, installing appropriate {@link KeymapWrapper} and {@link KeymapActionMap} objects in the {@link InputMap} and {@link ActionMap} parent chains, respectively, and fire a property change event with name "keymap".
voidsetMargin(Insets m)
voidsetNavigationFilter(NavigationFilter filter)
voidsetSelectedTextColor(Color newColor)
voidsetSelectionColor(Color newColor)
voidsetSelectionEnd(int end)
Selects the text from the selection start postion to the given position.
voidsetSelectionStart(int start)
Selects the text from the given postion to the selection end position.
voidsetText(String text)
voidsetUI(TextUI newUI)
This method sets the label's UI delegate.
voidupdateUI()
This method resets the label's UI delegate to the default UI for the current look and feel.
intviewToModel(Point pt)
voidwrite(Writer output)
Write the content of this component to the given stream.

Field Detail

DEFAULT_KEYMAP

public static final String DEFAULT_KEYMAP

FOCUS_ACCELERATOR_KEY

public static final String FOCUS_ACCELERATOR_KEY

Constructor Detail

JTextComponent

public JTextComponent()
Creates a new JTextComponent instance.

Method Detail

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.

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

See Also: JTextComponent getKeymap keymaps

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.

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 {@link #loadKeymap}, when resolving a set of {@link KeyBinding} objects against this component.

Returns: The set of available Actions on this component's {@link EditorKit}

See Also: TextUI getActions

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.

Returns: an array of listeners

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

See Also: JTextComponent JTextComponent keymaps

getKeymap

public Keymap getKeymap()
Get the current Keymap of this component.

Returns: The component's current Keymap

See Also: JTextComponent keymap

getMargin

public Insets getMargin()

getNavigationFilter

public NavigationFilter getNavigationFilter()

Since: 1.4

getPreferredScrollableViewportSize

public Dimension getPreferredScrollableViewportSize()

getScrollableBlockIncrement

public int getScrollableBlockIncrement(Rectangle visible, int orientation, int direction)

getScrollableTracksViewportHeight

public boolean getScrollableTracksViewportHeight()

getScrollableTracksViewportWidth

public boolean getScrollableTracksViewportWidth()

getScrollableUnitIncrement

public int getScrollableUnitIncrement(Rectangle visible, int orientation, int direction)

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)
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 {@link TextUI#getToolTipText(JTextComponent, Point)}.

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.

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 {@link 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

See Also: NAME Action actionName

modelToView

public Rectangle modelToView(int position)

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.

paste

public void paste()

read

public void read(Reader input, Object streamDescription)
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 {@link Document#StreamDescriptionProperty}.

Parameters: input an input stream to read from. streamDescription an object, describing the stream.

Throws: IOException if the reader throws it.

See Also: getDocument getProperty

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.

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

See Also: JTextComponent getKeymap keymaps

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 {@link KeymapWrapper} and {@link KeymapActionMap} objects in the {@link InputMap} and {@link ActionMap} parent chains, respectively, and fire a property change event with name "keymap".

See Also: getKeymap 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.

viewToModel

public int viewToModel(Point pt)

write

public void write(Writer output)
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.