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: http://fifesoft.com/rsyntaxtextarea<br>
 * Downloads: https://sourceforge.net/projects/rsyntaxtextarea
 */
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);
      textArea.setAntiAliasingEnabled(true);
      RTextScrollPane sp = new RTextScrollPane(textArea);
      sp.setFoldIndicatorEnabled(true);
      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);
         }
      });
   }

}

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 TextEditorDemo.java.

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 TextEditorDemo.java
Unix:    javac -classpath <path-to-jar>/rsyntaxtextarea.jar TextEditorDemo.java
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.