RSyntaxTextArea With Swing In Processing

You’re probably not going to get an editor with syntax coloring any easier than the following, even though it could prove challenging to some. It all starts with RSyntaxTextArea which may be found here:

The secret to success is to download the jar file. It’s ok to download the source code, but what you really need for Processing is the jar file. I found it by going first to StackOverflow:

Look for the post that looks like this:

and click on the ‘here’ . It should download the jar file to your system.

Now we just have to install the jar on Processing which isn’t too difficult if you follow this set of instructions:

First go to Documents/Processing/libraries folder (macos) and create a folder called ‘RSyntaxTextArea’. Inside this folder create another folder called ‘library’. Drag and drop the jar file that you downloaded into this folder.

Copy/paste the source code below into Processing; I’ve already loaded all the requisite files and hopefully you won’t have to do it again, but if you do, use this procedure: Go to the main menu bar and select ‘Sketch’ and scroll down in the drop down menu to ‘Import Library…’. You should see ‘RSyntaxTextArea’ in the list (which is the folder that you created) and select it. It should automatically add the requisite files to your current sketch. Now you should be able to run the demo and see output similar to that shown below. The editor was set to PYTHON initially and should color keywords for python. There are several other options besides python if you want to use the editor for a different language.

Source code for editor:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import org.fife.io.*;
import org.fife.print.*;
import org.fife.ui.rsyntaxtextarea.*;
import org.fife.ui.rsyntaxtextarea.focusabletip.*;
import org.fife.ui.rsyntaxtextarea.modes.*;
import org.fife.ui.rsyntaxtextarea.parser.*;
import org.fife.ui.rsyntaxtextarea.templates.*;
import org.fife.ui.rtextarea.*;
import org.fife.util.*;

JFrame frame;
RTextScrollPane scrlPane;
RSyntaxTextArea txtArea;

final int _wndW = 600;
final int _wndH = 600;

void textEditor() {
  txtArea = new RSyntaxTextArea();
  txtArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_PYTHON);
  //  textArea.setCodeFoldingEnabled(true);
  scrlPane = new RTextScrollPane(txtArea);
  scrlPane.setBounds(10, 50, _wndW-20, _wndH-90);
  frame.add(scrlPane);
}

void fileSelected(File selection) {
  if (selection == null) {
    println("Window was closed or the user hit cancel.");
  } else {
    String[] lines = loadStrings(selection.getAbsolutePath());
    for (int i = 0; i < lines.length; i++) {
      txtArea.append(lines[i] + '\n');
    }
  }
}

void openBtn(int x, int y, int w, int h) {
  JButton btn = new JButton("Open...");
  btn.setBounds(x, y, w, h);
  frame.add(btn);
  // **** Action **** //
  btn.addActionListener( new ActionListener() {
    void actionPerformed(ActionEvent actionEvent) {
      selectInput("Select file:", "fileSelected");
    }
  }
  );
  btn.repaint();
}

void buildWnd() {
  openBtn(50, 10, 90, 24);
  textEditor();
  frame.setVisible(true);
}

void setup() {
  surface.setVisible(false);
  frame = new JFrame();
  frame.setBounds(100, 100, _wndW, _wndH);
  frame.setTitle("RSyntaxTextArea Demo");
  frame.setLayout(null);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.addComponentListener(new java.awt.event.ComponentAdapter() {
    void componentResized(ComponentEvent e) {
      JFrame tmp = (JFrame)e.getSource();
      scrlPane.setBounds(10, 50, tmp.getWidth() - 20, tmp.getHeight() - 90);
    }
  }
  );

  SwingUtilities.invokeLater(new Runnable() {
    public void run() {
      buildWnd();   // Build components on the EventDispatchThread(EDT).
    }
  }
  );
}

Output: