RSyntaxTextArea - A Simple Example
Back to Example Index
Download as Eclipse project
Back to RSyntaxTextArea Home

The Example Source
Cobbling up a simple Java app that embeds RSyntaxTextArea is easy. Here is an exmaple:
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.<p>
 * This example uses RSyntaxTextArea 2.0.1.<p>
 * Project Home:<br>
 * Downloads:
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);
      RTextScrollPane sp = new RTextScrollPane(textArea);

      setTitle("Text Editor Demo");


   public static void main(String[] args) {
      // Start all Swing applications on the EDT.
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            new TextEditorDemo().setVisible(true);


Most of it is just Swing boilerplate.

An RSyntaxTextArea is the main text editor class. It extends JTextArea, so it has all the standard methods you'd expect from a Swing text component, plus more specific to handling syntax highlighting.

An RTextScrollPane is an extension of JScrollPane that supports line numbers. You can use a standard JScrollPane if you want, but when editing source code, it is often nice to have line numbering enabled.

Save this file as

Compiling the Example
For simplicity, we will just use javac on the command line to compile. Bring up a command prompt or shell, make sure javac is on your PATH, and run the following command:
Windows: javac -classpath <path-to-jar>\rsyntaxtextarea.jar
Unix:    javac -classpath <path-to-jar>/rsyntaxtextarea.jar
where <path-to-jar> is the path to the rsyntaxtextarea.jar file. This should yield no errors or warnings, and on completion there should see a file named TextEditorDemo.class in your current directory.
Running the Example
Running the example is just as simple as compiling it:
Windows: java -classpath <path-to-jar>\rsyntaxtextarea.jar;. TextEditorDemo
Unix:    java -classpath <path-to-jar>/rsyntaxtextarea.jar:. TextEditorDemo
A window should pop up, containing a text editor. When you type in the editor, the text will be highilghted as Java code.
Going a Little Further
RSyntaxTextArea knows about over 30 programming languages. To make the example highlight another language, simply change SyntaxConstants.SYNTAX_STYLE_JAVA to one of the other values defined in the SyntaxConstants class.