I'm writing a source-code editor in Java (for Java source code), and I'd like to add simple syntax highlighting (distinctive coloring for keywords would suffice). Any suggestions?
-
I don't know if I'd even use the "almost" in the phrase "almost incomprehensible". – Paul Sonier May 14 '09 at 17:39
-
1Tried to improve this in a way that supports mmyers' answer. – Shog9 May 14 '09 at 17:40
-
I just went with the "code for syntax highlighting" part and ignored the rest of it. I have no idea if that's what he's looking for or not. – Michael Myers May 14 '09 at 17:42
-
Question about Java syntax-highlighting libraries: http://stackoverflow.com/questions/221570/what-code-highlighting-libs-are-there-for-java – Jonik May 14 '09 at 17:43
-
Yeah. Funny thing is, if you type that into google, you get a bunch of results. One of them is this dupe: http://stackoverflow.com/questions/221570/what-code-highlighting-libs-are-there-for-java. – Erich Mirabal May 14 '09 at 17:44
-
1@Shog9: editing the question to fit an answer? Is that kosher? – Paul Sonier May 14 '09 at 17:46
-
1Voted down, you shouldn't use SO for questions that are easily answered after a google search. Also it was almost impossible to read – marcgg May 14 '09 at 17:49
-
4@MarcG: Not sure where you got that opinion, but you are rather wrong. That is /exactly/ what SO is for. – GEOCHET May 14 '09 at 17:52
-
1@Shog9 good rewrite!. The original was terrible... – Alex. S. May 14 '09 at 18:00
-
4I had voted it down previously because in its original form the question was utterly incomprehensible. And Marc G is completely wrong on his assertion that SO isn't for simple questions. Please refer to the newest podcast for Jeff & Joel's take. – TheTXI May 14 '09 at 18:02
-
OK, easy out, it's obvious that we're not dealing with a native english speaker. @MarcG where you got that impression? This is completely wrong. @Erich Mirabal this (the edited version) is a real and valid question. – Hejazzman May 14 '09 at 18:06
-
The question isn't clear about whether you want to write you're own syntax highlighting stuff or just using a lib. That's why you're getting quite different answers... – cort May 14 '09 at 18:12
-
@ktulur: The answers look fine for what they are asking. – GEOCHET May 14 '09 at 18:28
-
Interesting that the 4 libraries mentioned in the other "Java syntax-highlighting lib" question [http://stackoverflow.com/questions/221570/what-code-highlighting-libs-are-there-for-java] are totally different than the options so far mentioned here. – Jonik May 14 '09 at 18:34
-
@Rich B : before the rewrite it was impossible to read an the meaning of the question changed. I thought it was something different, so my bad. I undid my down vote – marcgg May 14 '09 at 18:46
-
(The way I understood it was "how do I put syntax highlighting in my IDE" or something like that.) – marcgg May 14 '09 at 18:48
-
1@MarcG: It really makes no difference how you understood it. Your original statement is flat out wrong. – GEOCHET May 14 '09 at 18:53
-
1@McWafflestix: There's some disagreement, but i hold that it is entirely appropriate in cases where the question is ambiguous. Remember, the goal is to create value for others to find and use in the future: therefore, a question should be specific and have specific answers; otherwise, it won't be found and re-used. I have no problem with editing questions to fit this vision - if i guess wrong, the original author can always ask another question, taking care to be more specific the second time around... – Shog9 May 14 '09 at 19:10
-
@Rich B: Ok I suppose you know better – marcgg May 14 '09 at 19:26
-
Wow, you guys clearly have no understanding of what he said. He said he wants to add syntax-highlighting to his text-editor, so simply suggesting him a method or some library to do it would have sufficed. – dnclem Dec 06 '11 at 07:06
6 Answers
Something like JSyntaxPane
, perhaps?
A very simple to use and extend JEditorKit that supports few languages. The main goal is to make it easy to have nice looking Java Swing Editors with support for Syntax Highlighting.

- 188,989
- 46
- 291
- 292
-
2This is not maintained anymore. Use [`RSyntaxTextArea`](https://github.com/bobbylight/RSyntaxTextArea) for new projects. – Navin Nov 10 '13 at 10:43
What about RSyntaxTextArea? It uses a modified BSD license.

- 169,610
- 28
- 168
- 175

- 1,088
- 1
- 11
- 20
-
1So bad? I'm using it in a project and I think it's great. Besides, If you want to code your own stuff you can study it as it's opensource. – cort May 14 '09 at 18:46
-
Tried and looks great, like the features like code folding. It does not support python, it is a pity. – chenyi1976 Mar 20 '12 at 01:33
-
sorry it support python. its website mislead me, and their applet too. – chenyi1976 Mar 20 '12 at 01:47
-
2This should be upvoted more often and accepted as an answer - it's afaik the only library that is still being maintained. – Dexter Feb 18 '13 at 15:06
You first should think about using a common parser to create an AST (abstract syntax tree) from the sources. There are some tools around, first I find googling the internet was javaparser. It looks like this parser also records line numbers and columns, so the AST from javaparser can be a nice model for the editor.
Just process the tree, define colors for the AST node types and print it.

- 113,398
- 19
- 180
- 268
Might want to look at an existing editor (Notepad++ for example - http://notepad-plus.sourceforge.net/uk/site.htm) and see how user-defined syntax highlighting is done (oneo of the plugins to check - Gmod 10 Lua Syntax Highlighter). I'd wager that the Java (and other languages) are done similarly...

- 161
- 1
- 8
You should check Google's prettify.js out. Some pretty neat tricks in there, and you might get a more robust feel for syntax highlighting.

- 21,119
- 15
- 74
- 98
-
2@ktulur: Did you actually look at it? It does syntax highlighting for many languages including Java. This will be a great way to learn how to write a syntax highlighting algorithm. Specifically one that can support multiple languages. – GEOCHET May 14 '09 at 17:52
-
Ok, sorry, I was thinking about using an existing component ,not about writing your own stuff. That's why when I've read ".js" I was like "wtf?" – cort May 14 '09 at 17:56
-
-
1Yep sometimes, but generally, isn't it better reuse? I think that in that way you can spend your time in your own original stuff. – cort May 14 '09 at 18:03
-
If that were the case, why on earth would he be writing a Java editor in the first place? – GEOCHET May 14 '09 at 18:07
-
There is a Java port of `prettify.js`; it is `java-prettify`: https://code.google.com/p/java-prettify/ – Jmini Jul 11 '14 at 12:04