Syntax Highlighting with / in the framework Swing

2

How to do a syntax highlighting , like what the IDEs use have, but in the / with Swing framework, with Java.

I intend to use syntax highlighting in several different keywords. How can I change the color of words according to their word and categories?

    
asked by anonymous 21.08.2014 / 03:44

1 answer

1

For the Swing framework there is RSyntaxTextArea that inherits from the javax.swing.JTextArea using a BSD derived license which allows you to use the code in both open and private code and has highlights for the following languages:

  • html
  • css
  • java
  • xml
  • sql
  • scala
  • ruby
  • python
  • php
  • perl
  • moon

(see full list)

One example, taken from the project's github is:

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

public class TextEditorDemo extends JFrame {

   public TextEditorDemo() {

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

      RSyntaxTextArea textArea = new RSyntaxTextArea(20, 60);
      textArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVA);
      textArea.setCodeFoldingEnabled(true);
      RTextScrollPane sp = new RTextScrollPane(textArea);
      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);
         }
      });
   }

}

To add new languages (your own keywords) there is this official document explaining and there is also this other project that is partner and aims to just add support for other languages .

    
22.08.2014 / 00:42