Interface IJTextComponent

All Superinterfaces:
IComponent, IContainer, IJComponent

public interface IJTextComponent extends IJComponent
  • Method Details

    • addCaretListener

      void addCaretListener(CaretListener listener)
      Adds a caret listener for notification of any changes to the caret.
      Parameters:
      listener - the listener to be added
      See Also:
    • removeCaretListener

      void removeCaretListener(CaretListener listener)
      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

      void setDocument(Document doc)
      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

      void setComponentOrientation(ComponentOrientation o)
    • 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

      void setMargin(Insets m)
      Sets margin space between the text component's border and its text. The text component's default Border object will use this value to create the proper margin. However, if a non-default border is set on the text component, it is that Border object'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
    • setNavigationFilter

      void setNavigationFilter(NavigationFilter filter)
      Sets the NavigationFilter. NavigationFilter is used by DefaultCaret and the default cursor movement actions as a way to restrict the cursor movement.
      Since:
      1.4
    • getNavigationFilter

      NavigationFilter getNavigationFilter()
      Returns the NavigationFilter. NavigationFilter is used by DefaultCaret and the default cursor movement actions as a way to restrict the cursor movement. A null return value implies the cursor movement and selection should not be restricted.
      Returns:
      the NavigationFilter
      Since:
      1.4
    • getCaret

      Caret getCaret()
      Fetches the caret that allows text-oriented navigation over the view.
      Returns:
      the caret
    • setCaret

      void setCaret(Caret c)
      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

      void setHighlighter(Highlighter h)
      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 to null to 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

      void setKeymap(Keymap map)
      Sets the keymap to use for binding events to actions. Setting to null effectively 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 the dragEnabled property, which must be true to enable automatic drag handling (the first part of drag and drop) on this component. The transferHandler property needs to be set to a non-null value for the drag to do anything. The default value of the dragEnabled property is false.

      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 true can 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 exportAsDrag method of a TransferHandler.

      Parameters:
      b - the value to set the dragEnabled property to
      Throws:
      HeadlessException - if b is true and GraphicsEnvironment.isHeadless() returns true bound: false
      Since:
      1.4
      See Also:
    • getDragEnabled

      boolean getDragEnabled()
      Gets the dragEnabled property.
      Returns:
      the value of the dragEnabled property
      Since:
      1.4
      See Also:
    • getKeymap

      Keymap getKeymap()
      Fetches the keymap currently active in this text component.
      Returns:
      the keymap
    • getCaretColor

      Color getCaretColor()
      Fetches the current color used to render the caret.
      Returns:
      the color
    • setCaretColor

      void setCaretColor(Color c)
      Sets the current color used to render the caret. Setting to null effectively 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

      void setSelectionColor(Color c)
      Sets the current color used to render the selection. Setting the color to null is the same as setting Color.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

      void setSelectedTextColor(Color c)
      Sets the current color used to render the selected text. Setting the color to null is the same as Color.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

      void setDisabledTextColor(Color c)
      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

      void replaceSelection(String content)
      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

      String getText(int offs, int len) throws BadLocationException
      Fetches a portion of the text represented by the component. Returns an empty string if length is 0.
      Parameters:
      offs - the offset >= 0
      len - the length >= 0
      Returns:
      the text
      Throws:
      BadLocationException - if the offset or length are invalid
    • modelToView

      Rectangle modelToView(int pos) throws BadLocationException
      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

      int viewToModel(Point pt)
      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 for null selections.
      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 for null selections.
      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 time setCaretPosition was called. This forms a selection. If the document is null, 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 for position is 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

      void read(Reader in, Object desc) throws IOException
      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 from
      desc - 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

      void write(Writer out) throws IOException
      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 the TextComponent. Note that the caret tracks change, so this may move if the underlying text of the component is changed. If the document is null, 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 for position is 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

      void setText(String t)
      Sets the text of this TextComponent to the specified text. If the text is null or 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 PropertyChangeEvent is fired when it changes. To listen for changes to the text, use DocumentListener.

      Parameters:
      t - the new text to be set
      See Also:
    • getText

      String getText()
      Returns the text contained in this TextComponent. If the underlying document is null, will give a NullPointerException.

      Note that text is not a bound property, so no PropertyChangeEvent is fired when it changes. To listen for changes to the text, use DocumentListener.

      Returns:
      the text
      Throws:
      NullPointerException - if the document is null
      See Also:
    • getSelectedText

      String getSelectedText()
      Returns the selected text contained in this TextComponent. If the selection is null or the document empty, returns null.
      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 this TextComponent is editable or not.
      Returns:
      the boolean value
      See Also:
    • setEditable

      void setEditable(boolean b)
      Sets the specified boolean to indicate whether or not this TextComponent should 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 the Caret implementation 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 the Caret implementation 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 setCaretPosition followed by a call to moveCaretPosition. The preferred way to manage selection is by calling those methods directly.

      Parameters:
      selectionStart - the start position of the text
      selectionEnd - the end position of the text
      See Also:
    • selectAll

      void selectAll()
      Selects all the text in the TextComponent. Does nothing on a null or empty document.
    • getToolTipText

      String getToolTipText(MouseEvent event)
      Returns the string to be used as the tooltip for event. This will return one of:
      1. If setToolTipText has been invoked with a non-null value, it will be returned, otherwise
      2. The value from invoking getToolTipText on the UI will be returned.
      By default JTextComponent does not register itself with the ToolTipManager. This means that tooltips will NOT be shown from the TextUI unless registerComponent has been invoked on the ToolTipManager.
      Parameters:
      event - the event in question
      Returns:
      the string to be used as the tooltip for event
      See Also:
    • 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 preferredSize of a JViewport whose view is this Scrollable
    • getScrollableUnitIncrement

      int getScrollableUnitIncrement(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. 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 viewport
      orientation - either SwingConstants.VERTICAL or SwingConstants.HORIZONTAL
      direction - 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

      int getScrollableBlockIncrement(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.

      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 viewport
      orientation - either SwingConstants.VERTICAL or SwingConstants.HORIZONTAL
      direction - 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 this Scrollable to 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 a Scrollable whose ancestor is a JScrollPane effectively 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 this Scrollable to 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 the AccessibleContext associated with this JTextComponent. For text components, the AccessibleContext takes the form of an AccessibleJTextComponent. A new AccessibleJTextComponent instance is created if necessary.
      Returns:
      an AccessibleJTextComponent that serves as the AccessibleContext of this JTextComponent