Like @MadProgrammer mentioned, you can use RSyntaxTextArea.
From its README:
RSyntaxTextArea is a customizable, syntax highlighting text component
for Java Swing applications. Out of the box, it supports syntax
highlighting for 50+ programming languages, code folding, search and
replace, and has add-on libraries for code completion and spell
checking. Syntax highlighting for additional languages can be added
via tools such as JFlex.
Example Usage:-
import javax.swing.*;
import java.awt.BorderLayout;
import org.fife.ui.rtextarea.*;
import org.fife.ui.rsyntaxtextarea.*;
public class TextEditorDemo extends JFrame {
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 TextEditorDemo().setVisible(true));
}
}