Class JEditTextArea

  • All Implemented Interfaces:
    java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable

    public class JEditTextArea
    extends javax.swing.JComponent
    jEdit's text area component. It is more suited for editing program source code than JEditorPane, because it drops the unnecessary features (images, variable-width lines, and so on) and adds a whole bunch of useful goodies such as:
    • More flexible key binding scheme
    • Supports macro recorders
    • Rectangular selection
    • Bracket highlighting
    • Syntax highlighting
    • Command repetition
    • Block caret can be enabled
    It is also faster and doesn't have as many problems. It can be used in other applications; the only other part of jEdit it depends on is the syntax package.

    To use it in your app, treat it like any other component, for example:

    JEditTextArea ta = new JEditTextArea();
     ta.setTokenMarker(new JavaTokenMarker());
     ta.setText("public class Test {\n"
         + "    public static void main(String[] args) {\n"
         + "        System.err.println(\"Hello World\");\n"
         + "    }\n"
         + "}");
    See Also:
    Serialized Form
    • Nested Class Summary

      • Nested classes/interfaces inherited from class javax.swing.JComponent

        javax.swing.JComponent.AccessibleJComponent
      • Nested classes/interfaces inherited from class java.awt.Container

        java.awt.Container.AccessibleAWTContainer
      • Nested classes/interfaces inherited from class java.awt.Component

        java.awt.Component.AccessibleAWTComponent, java.awt.Component.BaselineResizeBehavior, java.awt.Component.BltBufferStrategy, java.awt.Component.FlipBufferStrategy
    • Field Summary

      • Fields inherited from class javax.swing.JComponent

        listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
      • Fields inherited from class java.awt.Component

        accessibleContext, 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

      Constructors 
      Constructor Description
      JEditTextArea​(@NotNull TextAreaDefaults defaults, @NotNull SyntaxDocument document, boolean paintInvalid)
      Creates a new JEditTextArea with the specified settings.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void cut()
      Deletes the selected text from the text area and places it into the clipboard.
      int getCaretLine()
      Returns the caret line.
      int getCaretPosition()
      Returns the caret position.
      @NotNull javax.swing.text.Document getDocument()
      Returns the document this text area is editing.
      int getDocumentLength()
      Returns the length of the document.
      int getElectricScroll()
      Returns the number of lines from the top and button of the text area that are always visible.
      int getFirstLine()
      Returns the line displayed at the text area's origin.
      @NotNull InputHandler getInputHandler()
      Returns the input handler.
      int getLineCount()
      Returns the number of lines in the document.
      int getLineEndOffset​(int line)
      Returns the end offset of the specified line.
      int getLineStartOffset​(int line)
      Returns the start offset of the specified line.
      @NotNull java.lang.CharSequence getLineText​(int lineIndex)
      Returns the text on the specified line.
      int getMagicCaretPosition()
      Returns the `magic' caret position.
      int getMarkPosition()
      Returns the mark position.
      @Nullable java.lang.String getSelectedText()
      Returns the selected text, or null if no selection is active.
      int getSelectionEnd()
      Returns the selection end offset.
      int getSelectionStart()
      Returns the selection start offset.
      @NotNull java.lang.String getText()
      Returns the entire text of this text area.
      @Nullable java.lang.String getText​(int start, int len)
      Returns the specified substring of the document.
      void getText​(int start, int len, @NotNull javax.swing.text.Segment segment)
      Copies the specified substring of the document into a segment.
      int getVisibleLines()
      Returns the number of lines visible in this text area.
      boolean isEditable()
      Returns true if this text area is editable, false otherwise.
      boolean isModified()
      Return whether the text content has been modified from the "unmodified" state.
      boolean isOverwriteEnabled()
      Returns whether overwrite mode is active.
      boolean isSelectionRectangular()
      Returns true if the selection is rectangular, false otherwise.
      int lineToY​(int line)
      Converts a line index to a y co-ordinate.
      int offsetToX​(int line, int offset)
      Converts an offset in a line into an x co-ordinate.
      int offsetToX2​(int line, int offset)
      Converts an offset in a line into an x co-ordinate.
      void overwriteSetSelectedText​(@NotNull java.lang.String str)
      Similar to setSelectedText(), but overstrikes the appropriate number of characters if overwrite mode is enabled.
      void paste()
      Inserts the clipboard contents into the text.
      void processKeyEvent​(@NotNull java.awt.event.KeyEvent e)
      Forwards key events directly to the input handler.
      void removeNotify()
      Called by the AWT when this component is removed from it's parent.
      void resetModified()
      Reset the "modified" state.
      void select​(int start, int end)
      Selects from the start offset to the end offset.
      void selectAll()
      Selects all text in the document.
      void setCaretPosition​(int caret)
      Sets the caret position.
      void setEditingFocus()
      Sets the focus to this TextArea, so this component is instantly registered for key press events.
      void setFirstLine​(int firstLine)
      Sets the line displayed at the text area's origin without updating the scroll bars.
      void setFont​(@NotNull java.awt.Font font)
      Set the TextArea font
      void setMagicCaretPosition​(int magicCaret)
      Sets the `magic' caret position.
      void setOverwriteEnabled​(boolean overwrite)
      Sets whether overwrite mode is active.
      void setSelectedText​(@NotNull java.lang.String selectedText)
      Replaces the selection with the specified text.
      void setSelectionRectangular​(boolean rectangleSelect)
      Sets if the selection should be rectangular.
      void setText​(@NotNull java.lang.String text)
      Sets the entire text of this text area.
      void updateScrollBars()
      Updates the state of the scroll bars.
      int xToOffset​(int line, int x)
      Converts a x co-ordinate to an offset within a line.
      • Methods inherited from class javax.swing.JComponent

        addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getUI, getUIClassID, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, hide, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingOrigin, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, paramString, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update, updateUI
      • Methods inherited from class java.awt.Container

        add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusDownCycle, validate, validateTree
      • Methods inherited from class java.awt.Component

        action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAccessibleContext, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setMixingCutoutShape, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • JEditTextArea

        public JEditTextArea​(@NotNull
                             @NotNull TextAreaDefaults defaults,
                             @NotNull
                             @NotNull SyntaxDocument document,
                             boolean paintInvalid)
        Creates a new JEditTextArea with the specified settings.
        Parameters:
        defaults - the default settings
        paintInvalid - whether invalid lines should be painted as red tildes
    • Method Detail

      • setFont

        public void setFont​(@NotNull
                            @NotNull java.awt.Font font)
        Set the TextArea font
        Overrides:
        setFont in class javax.swing.JComponent
        Parameters:
        font - font
      • getInputHandler

        @NotNull
        public @NotNull InputHandler getInputHandler()
        Returns the input handler.
      • getElectricScroll

        public int getElectricScroll()
        Returns the number of lines from the top and button of the text area that are always visible.
      • updateScrollBars

        public void updateScrollBars()
        Updates the state of the scroll bars. This should be called if the number of lines in the document changes, or when the size of the text are changes.
      • getFirstLine

        public int getFirstLine()
        Returns the line displayed at the text area's origin.
      • setFirstLine

        public void setFirstLine​(int firstLine)
        Sets the line displayed at the text area's origin without updating the scroll bars.
      • getVisibleLines

        public int getVisibleLines()
        Returns the number of lines visible in this text area.
      • setEditingFocus

        public void setEditingFocus()
        Sets the focus to this TextArea, so this component is instantly registered for key press events. The graphics context must be fully initialized before calling this method.
      • lineToY

        public int lineToY​(int line)
        Converts a line index to a y co-ordinate.
        Parameters:
        line - the line
      • offsetToX

        public int offsetToX​(int line,
                             int offset)
        Converts an offset in a line into an x co-ordinate. This is a slow version that can be used any time.
        Parameters:
        line - the line
        offset - the offset, from the start of the line
      • offsetToX2

        public int offsetToX2​(int line,
                              int offset)
        Converts an offset in a line into an x co-ordinate. This is a fast version that should only be used if no changes were made to the text since the last repaint.
        Parameters:
        line - the line
        offset - the offset, from the start of the line
      • xToOffset

        public int xToOffset​(int line,
                             int x)
        Converts a x co-ordinate to an offset within a line.
        Parameters:
        line - the line
        x - the x co-ordinate
      • getDocument

        @NotNull
        public @NotNull javax.swing.text.Document getDocument()
        Returns the document this text area is editing.
      • getDocumentLength

        public int getDocumentLength()
        Returns the length of the document. Equivalent to calling getDocument().getLength().
      • getLineCount

        public int getLineCount()
        Returns the number of lines in the document.
      • getLineStartOffset

        public int getLineStartOffset​(int line)
        Returns the start offset of the specified line.
        Parameters:
        line - the line
        Returns:
        the start offset of the specified line, or -1 if the line is invalid
      • getLineEndOffset

        public int getLineEndOffset​(int line)
        Returns the end offset of the specified line.
        Parameters:
        line - the line
        Returns:
        the end offset of the specified line, or -1 if the line is invalid
      • getText

        @NotNull
        public @NotNull java.lang.String getText()
        Returns the entire text of this text area.
      • setText

        public void setText​(@NotNull
                            @NotNull java.lang.String text)
        Sets the entire text of this text area.
      • getText

        @Nullable
        public @Nullable java.lang.String getText​(int start,
                                                  int len)
        Returns the specified substring of the document.
        Parameters:
        start - the start offset
        len - the length of the substring
        Returns:
        the substring, or null if the offsets are invalid
      • getText

        public void getText​(int start,
                            int len,
                            @NotNull
                            @NotNull javax.swing.text.Segment segment)
        Copies the specified substring of the document into a segment. If the offsets are invalid, the segment will contain a null string.
        Parameters:
        start - the start offset
        len - the length of the substring
        segment - the segment
      • getLineText

        @NotNull
        public @NotNull java.lang.CharSequence getLineText​(int lineIndex)
        Returns the text on the specified line.
        Parameters:
        lineIndex - the line
        Returns:
        the text, or null if the line is invalid
      • getSelectionStart

        public int getSelectionStart()
        Returns the selection start offset.
      • getSelectionEnd

        public int getSelectionEnd()
        Returns the selection end offset.
      • getCaretPosition

        public int getCaretPosition()
        Returns the caret position. This will either be the selection start or the selection end, depending on which direction the selection was made in.
      • getCaretLine

        public int getCaretLine()
        Returns the caret line.
      • getMarkPosition

        public int getMarkPosition()
        Returns the mark position. This will be the opposite selection bound to the caret position.
        See Also:
        getCaretPosition()
      • setCaretPosition

        public void setCaretPosition​(int caret)
        Sets the caret position. The new selection will consist of the caret position only (hence no text will be selected).
        Parameters:
        caret - the caret position
        See Also:
        select(int, int)
      • selectAll

        public void selectAll()
        Selects all text in the document.
      • select

        public void select​(int start,
                           int end)
        Selects from the start offset to the end offset. This is the general selection method used by all other selecting methods. The caret position will be start if start < end, and end if end > start.
        Parameters:
        start - the start offset
        end - the end offset
      • getSelectedText

        @Nullable
        public @Nullable java.lang.String getSelectedText()
        Returns the selected text, or null if no selection is active.
      • setSelectedText

        public void setSelectedText​(@NotNull
                                    @NotNull java.lang.String selectedText)
        Replaces the selection with the specified text.
        Parameters:
        selectedText - the replacement text for the selection
      • isEditable

        public boolean isEditable()
        Returns true if this text area is editable, false otherwise.
      • getMagicCaretPosition

        public int getMagicCaretPosition()
        Returns the `magic' caret position. This can be used to preserve the column position when moving up and down lines.
      • setMagicCaretPosition

        public void setMagicCaretPosition​(int magicCaret)
        Sets the `magic' caret position. This can be used to preserve the column position when moving up and down lines.
        Parameters:
        magicCaret - the magic caret position
      • overwriteSetSelectedText

        public void overwriteSetSelectedText​(@NotNull
                                             @NotNull java.lang.String str)
        Similar to setSelectedText(), but overstrikes the appropriate number of characters if overwrite mode is enabled.
        Parameters:
        str - the string
        See Also:
        setSelectedText(String), isOverwriteEnabled()
      • isOverwriteEnabled

        public boolean isOverwriteEnabled()
        Returns whether overwrite mode is active.
        Returns:
        whether overwrite mode is active
      • setOverwriteEnabled

        public void setOverwriteEnabled​(boolean overwrite)
        Sets whether overwrite mode is active.
        Parameters:
        overwrite - whether overwrite mode is active
      • isSelectionRectangular

        public boolean isSelectionRectangular()
        Returns true if the selection is rectangular, false otherwise.
      • setSelectionRectangular

        public void setSelectionRectangular​(boolean rectangleSelect)
        Sets if the selection should be rectangular.
        Parameters:
        rectangleSelect - true if the selection should be rectangular, false otherwise
      • cut

        public void cut()
        Deletes the selected text from the text area and places it into the clipboard.
      • paste

        public void paste()
        Inserts the clipboard contents into the text.
      • removeNotify

        public void removeNotify()
        Called by the AWT when this component is removed from it's parent. This stops clears the currently focused component.
        Overrides:
        removeNotify in class javax.swing.JComponent
      • processKeyEvent

        public void processKeyEvent​(@NotNull
                                    @NotNull java.awt.event.KeyEvent e)
        Forwards key events directly to the input handler. This is slightly faster than using a KeyListener because some Swing overhead is avoided.
        Overrides:
        processKeyEvent in class javax.swing.JComponent
      • isModified

        public boolean isModified()
        Return whether the text content has been modified from the "unmodified" state.
        Returns:
        true if the text content has been modified, or false if it is unmodified
      • resetModified

        public void resetModified()
        Reset the "modified" state.