-1

I have a simple HTML text Editor which I'm trying to convert into an IDE. I'm finished with the basics (Like adding a file browser and running the project). The next obstacle I'm facing is adding themes.

I googled and found this question but it requires adding the text before changing its color.
What behaviour I want is similar to JetBrains Webstorm (Or IntelliJ IDE): enter image description here

This is what it looks like as of now: enter image description here

Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
Rishon_JR
  • 160
  • 11
  • 1
    An IDE requires you to type the text before it can "color" it. A [JTextPane](https://docs.oracle.com/javase/tutorial/uiswing/components/editorpane.html) uses a [StyledDocument](https://docs.oracle.com/javase/8/docs/api/javax/swing/text/StyledDocument.html). You have to modify the `StyledDocument` as the user types. – Gilbert Le Blanc Feb 18 '23 at 07:31
  • 1
    You're going to need to take deep dive into the `StyledDocment`, for [example](https://stackoverflow.com/questions/15916249/colorpane-grab-strings-of-different-character-possible/15916621#15916621) and [example](https://stackoverflow.com/questions/18948148/jeditorpane-set-foreground-color-for-different-words/18948340#18948340) but I just want to point out that syntax highlight is VERY complex – MadProgrammer Feb 18 '23 at 07:35
  • I will try viewing IntelliJ and webstorm's source code. – Rishon_JR Feb 18 '23 at 15:13
  • 1
    Highlighting is the easy part. Parsing the text to find the tokens to highlight is the hard part as you need to consider things like multi-line comments, keywords within quotes etc. You can check out my old attempt at a Java syntax highlighter: https://stackoverflow.com/a/27338679/131872 – camickr Feb 18 '23 at 17:29
  • Then maybe have a look at [RSyntaxTextArea](https://bobbylight.github.io/RSyntaxTextArea/) – MadProgrammer Feb 18 '23 at 22:26
  • @MadProgrammer Thanks, ill look at it.Gilbert Le Blanc Thank you too, now I get it... – Rishon_JR Feb 20 '23 at 06:58

1 Answers1

1

Like @MadProgrammer mentioned, you can use RSyntaxTextArea.

From its README:

RSyntaxTextArea is a customizable, syntax highlighting text component for Java Swing applications. Out of the box, it supports syntax highlighting for 50+ programming languages, code folding, search and replace, and has add-on libraries for code completion and spell checking. Syntax highlighting for additional languages can be added via tools such as JFlex.

Example Usage:-

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

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 TextEditorDemo().setVisible(true));
    }

}
Arsh Coder
  • 688
  • 9
  • 33