# Editor Components

> For the complete documentation index, see [llms.txt](https://plugins.jetbrains.com/docs/intellij/llms.txt).

##  `EditorTextField` 

Compared to [Swing JTextArea](https://docs.oracle.com/en/java/javase/24/docs/api/java.desktop/javax/swing/JTextArea.html), the IntelliJ Platform's editor component has a ton of advantages: syntax highlighting support, code completion, code folding, and much more. [Editors](editors.html) are normally displayed in editor tabs, but they can be embedded in dialogs or tool windows, too. This is enabled by the [EditorTextField](https://github.com/JetBrains/intellij-community/tree/idea/263.7307.200/platform/platform-impl/src/com/intellij/ui/EditorTextField.java) component.

The following attributes can be specified:

* The file type according to which the text in the text field is parsed;

* Whether the text field is read-only or editable;

* Whether the text field is single-line or multiline.

Further customizations are possible by subclassing and overriding `createEditor()` and applying [EditorCustomization](https://github.com/JetBrains/intellij-community/tree/idea/263.7307.200/platform/platform-impl/src/com/intellij/ui/EditorCustomization.java). Several commonly needed customization implementations exist, including:

* [SpellCheckingEditorCustomization](https://github.com/JetBrains/intellij-community/tree/idea/263.7307.200/spellchecker/src/com/intellij/spellchecker/ui/SpellCheckingEditorCustomization.java) disables spellchecking

* [HorizontalScrollBarEditorCustomization](https://github.com/JetBrains/intellij-community/tree/idea/263.7307.200/platform/platform-impl/src/com/intellij/ui/HorizontalScrollBarEditorCustomization.java) to turn on/off horizontal scrollbar

* [ErrorStripeEditorCustomization](https://github.com/JetBrains/intellij-community/tree/idea/263.7307.200/platform/platform-impl/src/com/intellij/ui/ErrorStripeEditorCustomization.java) to turn on/off error stripes on the right

`EditorTextField` has a number of subclasses that can be used as needed for additional features.

To use an editor as an input field in a dialog, consider using [LanguageTextField](https://github.com/JetBrains/intellij-community/tree/idea/263.7307.200/platform/platform-impl/src/com/intellij/ui/LanguageTextField.java) as it provides a more accessible API.

### Providing Completion

To add autocompletion to the editor, use [TextFieldWithCompletion](https://github.com/JetBrains/intellij-community/tree/idea/263.7307.200/platform/platform-impl/src/com/intellij/util/textCompletion/TextFieldWithCompletion.java). The constructor takes as an argument a class that implements [TextCompletionProvider](https://github.com/JetBrains/intellij-community/tree/idea/263.7307.200/platform/platform-impl/src/com/intellij/util/textCompletion/TextCompletionProvider.java) to provide autocompletion variants. Use [TextFieldCompletionProvider](https://github.com/JetBrains/intellij-community/tree/idea/263.7307.200/platform/lang-impl/src/com/intellij/util/TextFieldCompletionProvider.java) to create a custom provider. For this, override `addCompletionVariants()` and add completion variants using `CompletionResultSet.addElement()`.

See also [TextFieldCompletionProviderDumbAware](https://github.com/JetBrains/intellij-community/tree/idea/263.7307.200/platform/lang-impl/src/com/intellij/util/TextFieldCompletionProviderDumbAware.java) for completion even at the indexing stage.

See [Code Completion](code-completion.html) to learn more about completion.

### Java

> **Note:**
> If the plugin depends on Java functionality, see [Java](plugin-compatibility.html#java).

A common use case for `EditorTextField` is entering the name of a Java class or package. This can be accomplished with the following steps:

* Use [JavaCodeFragmentFactory.createReferenceCodeFragment()](https://github.com/JetBrains/intellij-community/tree/idea/263.7307.200/java/java-psi-api/src/com/intellij/psi/JavaCodeFragmentFactory.java) to create a code fragment representing the class or package name;

* Call [PsiDocumentManager.getDocument()](https://github.com/JetBrains/intellij-community/tree/idea/263.7307.200/platform/core-api/src/com/intellij/psi/PsiDocumentManager.java) to get the document corresponding to the code fragment;

* Pass the returned document to the [EditorTextField](https://github.com/JetBrains/intellij-community/tree/idea/263.7307.200/platform/platform-impl/src/com/intellij/ui/EditorTextField.java) constructor or its `setDocument()` method.

```JAVA
PsiFile psiFile = PsiDocumentManager.getInstance(project)
        .getPsiFile(editor.getDocument());
PsiElement element =
        psiFile.findElementAt(editor.getCaretModel().getOffset());

PsiExpressionCodeFragment code =
        JavaCodeFragmentFactory.getInstance(project)
        .createExpressionCodeFragment("", element, null, true);

Document document =
        PsiDocumentManager.getInstance(project).getDocument(code);

EditorTextField editorTextField =
        new EditorTextField(document, project, JavaFileType.INSTANCE);
```

#### Tips

* When creating more than one field, two separate documents are needed. This is accomplished by using separate instances of `PsiExpressionCodeFragment`.

* `setText()` no longer works for the input field. However, `createExpressionCodeFragment()` accepts the text for the field as an argument. The empty string can be replaced and create a new document in lieu of `setText()`.

* Instances of `JTextField` in the GUI builder can be replaced with a custom replacement component using the right click in the IDE. Make sure to use "Custom Create" so the initialization code works properly.

