Interface IJTextComponent
- All Superinterfaces:
IComponent, IContainer, IJComponent
-
Method Summary
Modifier and TypeMethodDescriptionvoidaddCaretListener(CaretListener listener) Adds a caret listener for notification of any changes to the caret.voidcopy()Transfers the currently selected range in the associated text model to the system clipboard, leaving the contents in the text model.voidcut()Transfers the currently selected range in the associated text model to the system clipboard, removing the contents from the model.Gets theAccessibleContextassociated with thisJTextComponent.Action[]Fetches the command list for the editor.getCaret()Fetches the caret that allows text-oriented navigation over the view.Fetches the current color used to render the caret.Returns an array of all the caret listeners registered on this text component.intReturns the position of the text insertion caret for the text component.Fetches the current color used to render the selected text.Fetches the model associated with the editor.booleanGets thedragEnabledproperty.charReturns the key accelerator that will cause the receiving text component to get the focus.Fetches the object responsible for making highlights.Fetches the keymap currently active in this text component.Returns the margin between the text component's border and its text.Returns theNavigationFilter.Returns the preferred size of the viewport for a view component.intgetScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) Components that display logical rows or columns should compute the scroll increment that will completely expose one block of rows or columns, depending on the value of orientation.booleanReturns true if a viewport should always force the height of thisScrollableto match the height of the viewport.booleanReturns true if a viewport should always force the width of thisScrollableto match the width of the viewport.intgetScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) Components that display logical rows or columns should compute the scroll increment that will completely expose one new row or column, depending on the value of orientation.Returns the selected text contained in thisTextComponent.Fetches the current color used to render the selected text.Fetches the current color used to render the selection.intReturns the selected text's end position.intReturns the selected text's start position.getText()Returns the text contained in thisTextComponent.getText(int offs, int len) Fetches a portion of the text represented by the component.getToolTipText(MouseEvent event) Returns the string to be used as the tooltip forevent.booleanReturns the boolean indicating whether thisTextComponentis editable or not.modelToView(int pos) Converts the given location in the model to a place in the view coordinate system.voidmoveCaretPosition(int pos) Moves the caret to a new position, leaving behind a mark defined by the last timesetCaretPositionwas called.voidpaste()Transfers the contents of the system clipboard into the associated text model.voidInitializes from a stream.voidremoveCaretListener(CaretListener listener) Removes a caret listener.voidvoidreplaceSelection(String content) Replaces the currently selected content with new content represented by the given string.voidselect(int selectionStart, int selectionEnd) Selects the text between the specified start and end positions.voidSelects all the text in theTextComponent.voidSets the caret to be used.voidSets the current color used to render the caret.voidsetCaretPosition(int position) Sets the position of the text insertion caret for theTextComponent.voidvoidSets the current color used to render the disabled text.voidsetDocument(Document doc) Associates the editor with a text document.voidsetDragEnabled(boolean b) Sets thedragEnabledproperty, which must betrueto enable automatic drag handling (the first part of drag and drop) on this component.voidsetEditable(boolean b) Sets the specified boolean to indicate whether or not thisTextComponentshould be editable.voidsetFocusAccelerator(char aKey) Sets the key accelerator that will cause the receiving text component to get the focus.voidSets the highlighter to be used.voidSets the keymap to use for binding events to actions.voidSets margin space between the text component's border and its text.voidsetNavigationFilter(NavigationFilter filter) Sets theNavigationFilter.voidSets the current color used to render the selected text.voidSets the current color used to render the selection.voidsetSelectionEnd(int selectionEnd) Sets the selection end to the specified position.voidsetSelectionStart(int selectionStart) Sets the selection start to the specified position.voidSets the text of thisTextComponentto the specified text.intviewToModel(Point pt) Converts the given place in the view coordinate system to the nearest representative location in the model.voidStores the contents of the model into the given stream.Methods inherited from interface IComponent
addFocusListener, addKeyListener, addMouseListener, getLocationOnScreen, isEnabled, removeFocusListener, removeKeyListener, removeMouseListener, repaint, setEnabledMethods inherited from interface IContainer
add, add, addContainerListener, getComponentAt, getLayout, removeContainerListener, setLayoutMethods inherited from interface IJComponent
addPropertyChangeListener, addPropertyChangeListener, getClientProperty, getComponentPopupMenu, putClientProperty, removePropertyChangeListener, removePropertyChangeListener, setBorder, setTransferHandler, updateUI
-
Method Details
-
addCaretListener
Adds a caret listener for notification of any changes to the caret.- Parameters:
listener- the listener to be added- See Also:
-
removeCaretListener
Removes a caret listener.- Parameters:
listener- the listener to be removed- See Also:
-
getCaretListeners
CaretListener[] getCaretListeners()Returns an array of all the caret listeners registered on this text component.- Returns:
- all of this component's
CaretListeners or an empty array if no caret listeners are currently registered - Since:
- 1.4
- See Also:
-
setDocument
Associates the editor with a text document. The currently registered factory is used to build a view for the document, which gets displayed by the editor after revalidation. A PropertyChange event ("document") is propagated to each listener.- Parameters:
doc- the document to display/edit bound: true expert: true- See Also:
-
getDocument
Document getDocument()Fetches the model associated with the editor. This is primarily for the UI to get at the minimal amount of state required to be a text editor. Subclasses will return the actual type of the model which will typically be something that extends Document.- Returns:
- the model
-
setComponentOrientation
-
getActions
Action[] getActions()Fetches the command list for the editor. This is the list of commands supported by the plugged-in UI augmented by the collection of commands that the editor itself supports. These are useful for binding to events, such as in a keymap.- Returns:
- the command list
-
setMargin
Sets margin space between the text component's border and its text. The text component's defaultBorderobject will use this value to create the proper margin. However, if a non-default border is set on the text component, it is thatBorderobject's responsibility to create the appropriate margin space (else this property will effectively be ignored). This causes a redraw of the component. A PropertyChange event ("margin") is sent to all listeners.- Parameters:
m- the space between the border and the text bound: true
-
getMargin
Insets getMargin()Returns the margin between the text component's border and its text.- Returns:
- the margin
-
getCaret
Caret getCaret()Fetches the caret that allows text-oriented navigation over the view.- Returns:
- the caret
-
setCaret
Sets the caret to be used. By default this will be set by the UI that gets installed. This can be changed to a custom caret if desired. Setting the caret results in a PropertyChange event ("caret") being fired.- Parameters:
c- the caret bound: true expert: true- See Also:
-
getHighlighter
Highlighter getHighlighter()Fetches the object responsible for making highlights.- Returns:
- the highlighter
-
setHighlighter
Sets the highlighter to be used. By default this will be set by the UI that gets installed. This can be changed to a custom highlighter if desired. The highlighter can be set tonullto disable it. A PropertyChange event ("highlighter") is fired when a new highlighter is installed.- Parameters:
h- the highlighter bound: true expert: true- See Also:
-
setKeymap
Sets the keymap to use for binding events to actions. Setting tonulleffectively disables keyboard input. A PropertyChange event ("keymap") is fired when a new keymap is installed.- Parameters:
map- the keymap bound: true- See Also:
-
setDragEnabled
void setDragEnabled(boolean b) Sets thedragEnabledproperty, which must betrueto enable automatic drag handling (the first part of drag and drop) on this component. ThetransferHandlerproperty needs to be set to a non-nullvalue for the drag to do anything. The default value of thedragEnabledproperty isfalse.When automatic drag handling is enabled, most look and feels begin a drag-and-drop operation whenever the user presses the mouse button over a selection and then moves the mouse a few pixels. Setting this property to
truecan therefore have a subtle effect on how selections behave.Some look and feels might not support automatic drag and drop; they will ignore this property. You can work around such look and feels by modifying the component to directly call the
exportAsDragmethod of aTransferHandler.- Parameters:
b- the value to set thedragEnabledproperty to- Throws:
HeadlessException- ifbistrueandGraphicsEnvironment.isHeadless()returnstruebound: false- Since:
- 1.4
- See Also:
-
getDragEnabled
boolean getDragEnabled()Gets thedragEnabledproperty.- Returns:
- the value of the
dragEnabledproperty - Since:
- 1.4
- See Also:
-
getKeymap
-
getCaretColor
-
setCaretColor
Sets the current color used to render the caret. Setting tonulleffectively restores the default color. Setting the color results in a PropertyChange event ("caretColor") being fired.- Parameters:
c- the color bound: true preferred: true- See Also:
-
getSelectionColor
Color getSelectionColor()Fetches the current color used to render the selection.- Returns:
- the color
-
setSelectionColor
Sets the current color used to render the selection. Setting the color tonullis the same as settingColor.white. Setting the color results in a PropertyChange event ("selectionColor").- Parameters:
c- the color bound: true preferred: true- See Also:
-
getSelectedTextColor
Color getSelectedTextColor()Fetches the current color used to render the selected text.- Returns:
- the color
-
setSelectedTextColor
Sets the current color used to render the selected text. Setting the color tonullis the same asColor.black. Setting the color results in a PropertyChange event ("selectedTextColor") being fired.- Parameters:
c- the color bound: true preferred: true- See Also:
-
getDisabledTextColor
Color getDisabledTextColor()Fetches the current color used to render the selected text.- Returns:
- the color
-
setDisabledTextColor
Sets the current color used to render the disabled text. Setting the color fires off a PropertyChange event ("disabledTextColor").- Parameters:
c- the color bound: true preferred: true- See Also:
-
replaceSelection
Replaces the currently selected content with new content represented by the given string. If there is no selection this amounts to an insert of the given text. If there is no replacement text this amounts to a removal of the current selection.This is the method that is used by the default implementation of the action for inserting content that gets bound to the keymap actions.
This method is thread safe, although most Swing methods are not. Please see Threads and Swing for more information.
- Parameters:
content- the content to replace the selection with
-
getText
Fetches a portion of the text represented by the component. Returns an empty string if length is 0.- Parameters:
offs- the offset >= 0len- the length >= 0- Returns:
- the text
- Throws:
BadLocationException- if the offset or length are invalid
-
modelToView
Converts the given location in the model to a place in the view coordinate system. The component must have a positive size for this translation to be computed (i.e. layout cannot be computed until the component has been sized). The component does not have to be visible or painted.- Parameters:
pos- the position >= 0- Returns:
- the coordinates as a rectangle, with (r.x, r.y) as the location in the coordinate system, or null if the component does not yet have a positive size.
- Throws:
BadLocationException- if the given position does not represent a valid location in the associated document- See Also:
-
viewToModel
Converts the given place in the view coordinate system to the nearest representative location in the model. The component must have a positive size for this translation to be computed (i.e. layout cannot be computed until the component has been sized). The component does not have to be visible or painted.- Parameters:
pt- the location in the view to translate- Returns:
- the offset >= 0 from the start of the document, or -1 if the component does not yet have a positive size.
- See Also:
-
cut
void cut()Transfers the currently selected range in the associated text model to the system clipboard, removing the contents from the model. The current selection is reset. Does nothing fornullselections.- See Also:
-
copy
void copy()Transfers the currently selected range in the associated text model to the system clipboard, leaving the contents in the text model. The current selection remains intact. Does nothing fornullselections.- See Also:
-
paste
void paste()Transfers the contents of the system clipboard into the associated text model. If there is a selection in the associated view, it is replaced with the contents of the clipboard. If there is no selection, the clipboard contents are inserted in front of the current insert position in the associated view. If the clipboard is empty, does nothing.- See Also:
-
moveCaretPosition
void moveCaretPosition(int pos) Moves the caret to a new position, leaving behind a mark defined by the last timesetCaretPositionwas called. This forms a selection. If the document isnull, does nothing. The position must be between 0 and the length of the component's text or else an exception is thrown.- Parameters:
pos- the position- Throws:
IllegalArgumentException- if the value supplied forpositionis less than zero or greater than the component's text length- See Also:
-
setFocusAccelerator
void setFocusAccelerator(char aKey) Sets the key accelerator that will cause the receiving text component to get the focus. The accelerator will be the key combination of the alt key and the character given (converted to upper case). By default, there is no focus accelerator key. Any previous key accelerator setting will be superseded. A '\0' key setting will be registered, and has the effect of turning off the focus accelerator. When the new key is set, a PropertyChange event (FOCUS_ACCELERATOR_KEY) will be fired.- Parameters:
aKey- the key bound: true- See Also:
-
getFocusAccelerator
char getFocusAccelerator()Returns the key accelerator that will cause the receiving text component to get the focus. Return '\0' if no focus accelerator has been set.- Returns:
- the key
-
read
Initializes from a stream. This creates a model of the type appropriate for the component and initializes the model from the stream. By default this will load the model as plain text. Previous contents of the model are discarded.- Parameters:
in- the stream to read fromdesc- an object describing the stream; this might be a string, a File, a URL, etc. Some kinds of documents (such as html for example) might be able to make use of this information; if non-null, it is added as a property of the document- Throws:
IOException- as thrown by the stream being used to initialize- See Also:
-
write
Stores the contents of the model into the given stream. By default this will store the model as plain text.- Parameters:
out- the output stream- Throws:
IOException- on any I/O error
-
removeNotify
void removeNotify() -
setCaretPosition
void setCaretPosition(int position) Sets the position of the text insertion caret for theTextComponent. Note that the caret tracks change, so this may move if the underlying text of the component is changed. If the document isnull, does nothing. The position must be between 0 and the length of the component's text or else an exception is thrown.- Parameters:
position- the position- Throws:
IllegalArgumentException- if the value supplied forpositionis less than zero or greater than the component's text length
-
getCaretPosition
int getCaretPosition()Returns the position of the text insertion caret for the text component.- Returns:
- the position of the text insertion caret for the text component >= 0
-
setText
Sets the text of thisTextComponentto the specified text. If the text isnullor empty, has the effect of simply deleting the old text. When text has been inserted, the resulting caret location is determined by the implementation of the caret class.This method is thread safe, although most Swing methods are not. Please see Threads and Swing for more information.
Note that text is not a bound property, so no
PropertyChangeEventis fired when it changes. To listen for changes to the text, useDocumentListener.- Parameters:
t- the new text to be set- See Also:
-
getText
String getText()Returns the text contained in thisTextComponent. If the underlying document isnull, will give aNullPointerException.Note that text is not a bound property, so no
PropertyChangeEventis fired when it changes. To listen for changes to the text, useDocumentListener.- Returns:
- the text
- Throws:
NullPointerException- if the document isnull- See Also:
-
getSelectedText
String getSelectedText()Returns the selected text contained in thisTextComponent. If the selection isnullor the document empty, returnsnull.- Returns:
- the text
- Throws:
IllegalArgumentException- if the selection doesn't have a valid mapping into the document for some reason- See Also:
-
isEditable
boolean isEditable()Returns the boolean indicating whether thisTextComponentis editable or not.- Returns:
- the boolean value
- See Also:
-
setEditable
void setEditable(boolean b) Sets the specified boolean to indicate whether or not thisTextComponentshould be editable. A PropertyChange event ("editable") is fired when the state is changed.- Parameters:
b- the boolean to be set bound: true- See Also:
-
getSelectionStart
int getSelectionStart()Returns the selected text's start position. Return 0 for an empty document, or the value of dot if no selection.- Returns:
- the start position >= 0
-
setSelectionStart
void setSelectionStart(int selectionStart) Sets the selection start to the specified position. The new starting point is constrained to be before or at the current selection end.This is available for backward compatibility to code that called this method on
java.awt.TextComponent. This is implemented to forward to theCaretimplementation which is where the actual selection is maintained.- Parameters:
selectionStart- the start position of the text >= 0
-
getSelectionEnd
int getSelectionEnd()Returns the selected text's end position. Return 0 if the document is empty, or the value of dot if there is no selection.- Returns:
- the end position >= 0
-
setSelectionEnd
void setSelectionEnd(int selectionEnd) Sets the selection end to the specified position. The new end point is constrained to be at or after the current selection start.This is available for backward compatibility to code that called this method on
java.awt.TextComponent. This is implemented to forward to theCaretimplementation which is where the actual selection is maintained.- Parameters:
selectionEnd- the end position of the text >= 0
-
select
void select(int selectionStart, int selectionEnd) Selects the text between the specified start and end positions.This method sets the start and end positions of the selected text, enforcing the restriction that the start position must be greater than or equal to zero. The end position must be greater than or equal to the start position, and less than or equal to the length of the text component's text.
If the caller supplies values that are inconsistent or out of bounds, the method enforces these constraints silently, and without failure. Specifically, if the start position or end position is greater than the length of the text, it is reset to equal the text length. If the start position is less than zero, it is reset to zero, and if the end position is less than the start position, it is reset to the start position.
This call is provided for backward compatibility. It is routed to a call to
setCaretPositionfollowed by a call tomoveCaretPosition. The preferred way to manage selection is by calling those methods directly.- Parameters:
selectionStart- the start position of the textselectionEnd- the end position of the text- See Also:
-
selectAll
void selectAll()Selects all the text in theTextComponent. Does nothing on anullor empty document. -
getToolTipText
Returns the string to be used as the tooltip forevent. This will return one of:- If
setToolTipTexthas been invoked with a non-nullvalue, it will be returned, otherwise - The value from invoking
getToolTipTexton the UI will be returned.
JTextComponentdoes not register itself with theToolTipManager. This means that tooltips will NOT be shown from theTextUIunlessregisterComponenthas been invoked on theToolTipManager.- Parameters:
event- the event in question- Returns:
- the string to be used as the tooltip for
event - See Also:
- If
-
getPreferredScrollableViewportSize
Dimension getPreferredScrollableViewportSize()Returns the preferred size of the viewport for a view component. This is implemented to do the default behavior of returning the preferred size of the component.- Returns:
- the
preferredSizeof aJViewportwhose view is thisScrollable
-
getScrollableUnitIncrement
Components that display logical rows or columns should compute the scroll increment that will completely expose one new row or column, depending on the value of orientation. Ideally, components should handle a partially exposed row or column by returning the distance required to completely expose the item.The default implementation of this is to simply return 10% of the visible area. Subclasses are likely to be able to provide a much more reasonable value.
- Parameters:
visibleRect- the view area visible within the viewportorientation- eitherSwingConstants.VERTICALorSwingConstants.HORIZONTALdirection- less than zero to scroll up/left, greater than zero for down/right- Returns:
- the "unit" increment for scrolling in the specified direction
- Throws:
IllegalArgumentException- for an invalid orientation- See Also:
-
getScrollableBlockIncrement
Components that display logical rows or columns should compute the scroll increment that will completely expose one block of rows or columns, depending on the value of orientation.The default implementation of this is to simply return the visible area. Subclasses will likely be able to provide a much more reasonable value.
- Parameters:
visibleRect- the view area visible within the viewportorientation- eitherSwingConstants.VERTICALorSwingConstants.HORIZONTALdirection- less than zero to scroll up/left, greater than zero for down/right- Returns:
- the "block" increment for scrolling in the specified direction
- Throws:
IllegalArgumentException- for an invalid orientation- See Also:
-
getScrollableTracksViewportWidth
boolean getScrollableTracksViewportWidth()Returns true if a viewport should always force the width of thisScrollableto match the width of the viewport. For example a normal text view that supported line wrapping would return true here, since it would be undesirable for wrapped lines to disappear beyond the right edge of the viewport. Note that returning true for aScrollablewhose ancestor is aJScrollPaneeffectively disables horizontal scrolling.Scrolling containers, like
JViewport, will use this method each time they are validated.- Returns:
- true if a viewport should force the
Scrollables width to match its own
-
getScrollableTracksViewportHeight
boolean getScrollableTracksViewportHeight()Returns true if a viewport should always force the height of thisScrollableto match the height of the viewport. For example a columnar text view that flowed text in left to right columns could effectively disable vertical scrolling by returning true here.Scrolling containers, like
JViewport, will use this method each time they are validated.- Returns:
- true if a viewport should force the Scrollables height to match its own
-
getAccessibleContext
AccessibleContext getAccessibleContext()Gets theAccessibleContextassociated with thisJTextComponent. For text components, theAccessibleContexttakes the form of anAccessibleJTextComponent. A newAccessibleJTextComponentinstance is created if necessary.- Returns:
- an
AccessibleJTextComponentthat serves as theAccessibleContextof thisJTextComponent
-