Syntax Highlighting with/in the Swing framework

Asked

Viewed 121 times

2

How to make a syntax Highlighting, like what Ides use, but in/with Swing framework, with Java.

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

  • 7

    I think you’ll need to put a little more detail into your question. Like, what kind of application is it (web, windows) what frameworks do you use (swing? RCP?) Just say Java gets a little vague.

  • True, I will change... I forget that there is not only Java Swing in the world

  • Even for desktop still exist Javafx (the current standard); AWT,SWT, Swingx, Griffon and even the Openswing

  • Javafx can be used together with Swing without problems?

  • Yes, (provided you use Javafx 8). In which there is even a component called Swingnode which exists precisely to facilitate integration.

  • 1

    I edited your topic so that it can be reopened, but I suggest you edit it again to add more information about the last sentence. For example: What would categories be? How would keywords relate to them?

Show 1 more comment

1 answer

1

For the Swing framework there is Rsyntaxtextarea who inherits from the class javax.swing.Jtextarea who uses a license derived from BSD which allows you to use the code both in open and private code and has Highlights for the following languages:

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

(see the full list)

An example, taken from github of the project 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 a partner and aims to add support to other languages.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.