RSyntaxTextArea - Changing the Color Scheme
Back to Example Index
Download as Eclipse project
Back to RSyntaxTextArea Home


Description

NOTE: Changing the color scheme will be simplified in (hopefully!) the next release.

What if you don't like the colors RSyntaxTextArea uses to highlight code by default? What if the default font isn't to your liking? OS X users would probably like to use Monaco or Menlo.

Changing the color scheme (or fonts used) can currently only be done programmatically. Care must be taken because, in the default scheme, keywords are bold and comments are italicized - in other words, these token types use a different java.awt.Font than the default. This means when changing the font, you'll have to make more than a single method call.

API Overview

As with all JTextComponents, an RSyntaxTextArea's base font is accessed with getFont() and setFont(Font). This is the font that is used for a token type (i.e. keyword, variable) if no font is specified for that type. Similarly, the text area's foreground color (getForeground()/setForeground(Color) is used as the color for a token type if no foreground color is explicitly specified.

Token type-specific colors and fonts are specified in an RSyntaxTextArea's SyntaxScheme. You can grab an RSTA's SyntaxScheme via the getSyntaxScheme() and setSyntaxScheme(SyntaxScheme) methods. It has a public field named styles, which is an array of Style objects. You reference a Style for a particular token type by using one of the integer constants defined in the Token class, such as Token.RESERVED_WORD or Token.COMMENT_EOL.

A Style has public fields allowing you to change the following properties of a token type and how it is painted:

  • background - The background color to use. If this is null, the background for the token will not be painted.
  • font - The font to use for this token type. If this is null, the text area's default font (i.e. getFont()) will be used.
  • foreground - The foreground color to use. If this is null, the text area's foreground color (i.e. getForeground()) will be used.
  • underline - Whether this token type should be underlined.

Modifying these fields (and revalidating the text area via "textArea.revalidate(); textArea.repaint();") will cause the RSTA to take the changes at runtime.

The Example Source
import java.awt.*;
import javax.swing.*;

import org.fife.ui.rtextarea.*;
import org.fife.ui.rsyntaxtextarea.*;

public class SyntaxSchemeDemo extends JFrame {

   private static final long serialVersionUID = 1L;

   private static final String text =
      "public class ExampleSource {\n\n" +
      "   // Check out the crazy modified styles!\n" +
      "   public static void main(String[] args) {\n" +
      "      System.out.println(\"Hello, world!\");\n" +
      "   }\n\n" +
      "}\n";

   public SyntaxSchemeDemo() {

      JPanel cp = new JPanel(new BorderLayout());

      RSyntaxTextArea textArea = new RSyntaxTextArea(20, 60);
      textArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVA);
      RTextScrollPane sp = new RTextScrollPane(textArea);
      cp.add(sp);

      textArea.setText(text);

      // Set the font for all token types.
      setFont(textArea, new Font("Comic Sans MS", Font.PLAIN, 16));

      // Change a few things here and there.
      SyntaxScheme scheme = textArea.getSyntaxScheme();
      scheme.styles[Token.RESERVED_WORD].background = Color.pink;
      scheme.styles[Token.DATA_TYPE].foreground = Color.blue;
      scheme.styles[Token.LITERAL_STRING_DOUBLE_QUOTE].underline = true;
      scheme.styles[Token.COMMENT_EOL].font = new Font("Georgia", Font.ITALIC, 12);

      setContentPane(cp);
      setTitle("Syntax Scheme Demo");
      setDefaultCloseOperation(EXIT_ON_CLOSE);
      pack();
      setLocationRelativeTo(null);

   }

   /**
    * Set the font for all token types.
    *
    * @param textArea The text area to modify.
    * @param font The font to use.
    */
   public static void setFont(RSyntaxTextArea textArea, Font font) {
      if (font!=null) {
         SyntaxScheme ss = textArea.getSyntaxScheme();
         ss = (SyntaxScheme)ss.clone();
         for (int i=0; i<ss.styles.length; i++) {
            if (ss.styles[i]!=null) {
               ss.styles[i].font = font;
          }
       }
       textArea.setSyntaxScheme(ss);
       textArea.setFont(font);
      }
   }

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

}

Save this file as SyntaxSchemeDemo.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:
javac -cp <path-to-jar>\rsyntaxtextarea.jar SyntaxStyleDemo.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 SyntaxStyleDemo.class in your current directory.
Running the Example
Running the example is just as simple as compiling it:
java -cp <path-to-jar>\rsyntaxtextarea.jar;. SyntaxStyleDemo

A window should pop up, containing a text editor. As you type, the text should be syntax highlighted as Java code. The default RSTA color scheme has been modified to reflect the changes in the source.