Compared to Swing
JTextArea, the IntelliJ Platform's editor component has a ton of advantages: syntax highlighting support, code completion, code folding, and much more. Editors are normally displayed in editor tabs, but they can be embedded in dialogs or tool windows, too. This is enabled by the
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. Several commonly needed customization implementations exist, including:
HorizontalScrollBarEditorCustomizationto turn on/off horizontal scrollbar
ErrorStripeEditorCustomizationto turn on/off error stripes on the right
EditorTextField has a number of subclasses that can be used as needed for additional features.
If you want to use an editor as an input field in a dialog, then consider using
LanguageTextField as it provides a more accessible API.
If you want to add autocompletion to the editor, then use
TextFieldWithCompletion. The constructor takes as an argument a class that implements
TextCompletionProvider to provide autocompletion variants. Use
TextFieldCompletionProvider to create your own provider. For this, override
addCompletionVariants() and add completion variants using
TextFieldCompletionProviderDumbAware for completion even at the indexing stage.
See Code Completion to learn more about completion.
A common use case for
EditorTextField is entering the name of a Java class or package. This can be accomplished with the following steps:
JavaCodeFragmentFactory.createReferenceCodeFragment()to create a code fragment representing the class or package name;
PsiDocumentManager.getDocument()to get the document corresponding to the code fragment;
Pass the returned document to the
EditorTextFieldconstructor or its
When creating more than one field, two separate documents are needed. This is accomplished by using separate instances of
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
JTextFieldin the GUI builder can be replaced with a custom replacement component using the right click in your IDE. Make sure to use "Custom Create" so the initialization code works properly.