RSyntaxTextArea is a syntax highlighting, code folding text component for Java Swing. It extends JTextComponent so it integrates completely with the standard javax.swing.text package. It is fast and efficient, and can be used in any application that needs to edit or view source code.
RSTA supports syntax highlighting for 40+ programming languages out of the box, as well as code folding for many of them. But you're not limited to well-known languages. Highlighting and folding can be added for custom languages and plugged in with ease.
It can also be extended to provide an IDE-like experience. Parsers can be plugged in to listen for code modifications and denote errors or warnings with squiggle underlines. Sister libraries can be used to provide language-aware code completion and spell checking. Focusable tool tips can display relevant documentation for methods or objects while editing.
This library only requires Java 8 or greater, so it's ready to use in any application.
Jar downloads can be found on SourceForge, or you can clone the source from GitHub.
It is available under a modified BSD license.
import java.awt.*; import javax.swing.*; import org.fife.ui.rtextarea.*; import org.fife.ui.rsyntaxtextarea.*; /** * A simple example showing how to use RSyntaxTextArea to add Java syntax * highlighting to a Swing application. */ public class TextEditorDemo extends JFrame { private static final long serialVersionUID = 1L; public TextEditorDemo() { JPanel cp = new JPanel(new BorderLayout()); RSyntaxTextArea textArea = new RSyntaxTextArea(20, 60); textArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVA); textArea.setCodeFoldingEnabled(true); RTextScrollPane sp = new RTextScrollPane(textArea); cp.add(sp); setContentPane(cp); setTitle("Text Editor Demo"); setDefaultCloseOperation(EXIT_ON_CLOSE); pack(); setLocationRelativeTo(null); } public static void main(String[] args) { // Start all Swing applications on the EDT. SwingUtilities.invokeLater(new Runnable() { public void run() { new TextEditorDemo().setVisible(true); } }); } }