Class CodeArea

All Implemented Interfaces:
Styleable, EventTarget, Skinnable

public class CodeArea extends RichTextArea
CodeArea is an editable text component which supports styling (for example, syntax highlighting) of plain text.

Unlike its base class RichTextArea, the CodeArea requires a special kind of model to be used, a CodeTextModel.

Creating a CodeArea

The following example creates an editable control with the default CodeArea:
   CodeArea codeArea = new CodeArea();
   codeArea.setWrapText(true);
   codeArea.setLineNumbersEnabled(true);
   codeArea.setText("Lorem\nIpsum");
 
Which results in the following visual representation:

Image of the CodeArea control

Usage Considerations

CodeArea extends the RichTextArea class, meaning most of the functionality works as it does in the base class. There are some differences that should be mentioned:
  • Model behavior: any direct changes to the styling, such as applyStyle(), will be ignored
  • Line numbers: the CodeArea sets the RichTextArea.leftDecoratorProperty() to support the line numbers, so applications should not set or bind that property.
Since:
24
  • Property Details

  • Constructor Details

    • CodeArea

      public CodeArea(CodeTextModel model)
      This constructor creates the CodeArea with the specified CodeTextModel.
      Parameters:
      model - the instance of CodeTextModel to use
    • CodeArea

      public CodeArea()
      This constructor creates the CodeArea with the default CodeTextModel.
  • Method Details

    • setSyntaxDecorator

      public final void setSyntaxDecorator(SyntaxDecorator d)
      This convenience method sets the decorator property in the CodeTextModel. Nothing is done if the model is null.
      Parameters:
      d - the syntax decorator
      See Also:
    • getSyntaxDecorator

      public final SyntaxDecorator getSyntaxDecorator()
      This convenience method returns the syntax decorator value in the CodeTextModel, or null if the model is null.
      Returns:
      the syntax decorator value, or null
    • lineNumbersEnabledProperty

      public final BooleanProperty lineNumbersEnabledProperty()
      Determines whether to show line numbers. Toggling this property results in changes made to the leftDecorator property, so the application code should not bind or modify that property.
      Default value:
      false
      Returns:
      the line numbers enabled property
      See Also:
    • isLineNumbersEnabled

      public final boolean isLineNumbersEnabled()
      Gets the value of the lineNumbersEnabled property.
      Property description:
      Determines whether to show line numbers. Toggling this property results in changes made to the leftDecorator property, so the application code should not bind or modify that property.
      Default value:
      false
      Returns:
      the value of the lineNumbersEnabled property
      See Also:
    • setLineNumbersEnabled

      public final void setLineNumbersEnabled(boolean on)
      Sets the value of the lineNumbersEnabled property.
      Property description:
      Determines whether to show line numbers. Toggling this property results in changes made to the leftDecorator property, so the application code should not bind or modify that property.
      Default value:
      false
      Parameters:
      on - the value for the lineNumbersEnabled property
      See Also:
    • tabSizeProperty

      public final IntegerProperty tabSizeProperty()
      The size of a tab stop in spaces. Values less than 1 are treated as 1.
      Default value:
      8
      Returns:
      the tab size property
      See Also:
    • getTabSize

      public final int getTabSize()
      Gets the value of the tabSize property.
      Property description:
      The size of a tab stop in spaces. Values less than 1 are treated as 1.
      Default value:
      8
      Returns:
      the value of the tabSize property
      See Also:
    • setTabSize

      public final void setTabSize(int spaces)
      Sets the value of the tabSize property.
      Property description:
      The size of a tab stop in spaces. Values less than 1 are treated as 1.
      Default value:
      8
      Parameters:
      spaces - the value for the tabSize property
      See Also:
    • fontProperty

      public final ObjectProperty<Font> fontProperty()
      The font to use for text in the CodeArea.
      Default value:
      the Monospaced font with the default size
      Returns:
      the font property
      See Also:
    • setFont

      public final void setFont(Font value)
      Sets the value of the font property.
      Property description:
      The font to use for text in the CodeArea.
      Default value:
      the Monospaced font with the default size
      Parameters:
      value - the value for the font property
      See Also:
    • getFont

      public final Font getFont()
      Gets the value of the font property.
      Property description:
      The font to use for text in the CodeArea.
      Default value:
      the Monospaced font with the default size
      Returns:
      the value of the font property
      See Also:
    • lineSpacingProperty

      public final DoubleProperty lineSpacingProperty()
      Defines the vertical space in pixels between lines.
      Default value:
      0
      Returns:
      the property instance
      See Also:
    • setLineSpacing

      public final void setLineSpacing(double spacing)
      Sets the value of the lineSpacing property.
      Property description:
      Defines the vertical space in pixels between lines.
      Default value:
      0
      Parameters:
      spacing - the value for the lineSpacing property
      See Also:
    • getLineSpacing

      public final double getLineSpacing()
      Gets the value of the lineSpacing property.
      Property description:
      Defines the vertical space in pixels between lines.
      Default value:
      0
      Returns:
      the value of the lineSpacing property
      See Also:
    • getClassCssMetaData

      public static List<CssMetaData<? extends Styleable,?>> getClassCssMetaData()
      Gets the CssMetaData associated with this class, which may include the CssMetaData of its superclasses.
      Returns:
      the CssMetaData
    • getText

      public final String getText()
      Returns plain text. This method returns an empty string when the model is null.
      Returns:
      plain text
    • setText

      public final void setText(String text)
      Replaces text in this CodeArea.

      The caret gets reset to the start of the document, selection gets cleared, and an undo event gets created.

      Parameters:
      text - the text string