CodeFolding in RSyntaxTextArea

The following source code demonstrates code folding in RSyntaxTextArea. I downloaded rsyntaxtextarea-3.6.0.jar from the link below and installed it in Processing’s ‘libraries’ folder in a folder that I created and entitled ‘RSyntaxTextArea’. Inside of this folder I created another folder entitled ‘library’ and copy/pasted the .jar file into it.

I found a recent rsyntaxtextarea-3.6.0.jar here: https://repo1.maven.org/maven2/com/fifesoft/rsyntaxtextarea/3.6.0/

If you use an older .jar file, code folding may not work; it was not present in earlier .jar files. I’ve also included source code for a file that you may use for testing.

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

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

JFrame frame;
RTextScrollPane txtScrlPane;
RSyntaxTextArea txtArea;

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

void buildWnd() {
  surface.setVisible(false);
  frame = new JFrame();
  frame.setBounds(100, 100, _wndW, _wndH);
  frame.setTitle("RSyntaxTextArea Demo");
  JPanel panel = new JPanel(new BorderLayout());
  txtArea = new RSyntaxTextArea();
  txtArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVA);
  txtArea.setCodeFoldingEnabled(true);
  txtScrlPane = new RTextScrollPane(txtArea);
  panel.add(txtScrlPane);
  frame.setContentPane(panel);
  frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  frame.setVisible(true);
}

void setup() {
  SwingUtilities.invokeLater(() -> {
    buildWnd();
  }
  );
}

File for testing (copy/paste code into app created above):

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

JFrame frame;

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

void buildWnd() {
  frame = new JFrame();
  frame.setBounds(100, 100, _wndW, _wndH);
  frame.setTitle("JFrame Demo");
  frame.setLayout(null);
  frame.setVisible(true);
}

void setup() {
  surface.setVisible(false);
  javax.swing.SwingUtilities.invokeLater(()-> {
    buildWnd(); // Builds components on EventDispatchThread
  }
  );
}

CodeFolding Output:

1 Like