28

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?

20
  • 1
    Tried to improve this in a way that supports mmyers' answer. Commented May 14, 2009 at 17:40
  • 1
    @Shog9: editing the question to fit an answer? Is that kosher? Commented May 14, 2009 at 17:46
  • 1
    Voted down, you shouldn't use SO for questions that are easily answered after a google search. Also it was almost impossible to read Commented May 14, 2009 at 17:49
  • 4
    @MarcG: Not sure where you got that opinion, but you are rather wrong. That is /exactly/ what SO is for. Commented May 14, 2009 at 17:52
  • 4
    I 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. Commented May 14, 2009 at 18:02

6 Answers 6

22

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This is not maintained anymore. Use RSyntaxTextArea for new projects.
14

What about RSyntaxTextArea? It uses a modified BSD license.

4 Comments

So 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.
Tried and looks great, like the features like code folding. It does not support python, it is a pity.
sorry it support python. its website mislead me, and their applet too.
This should be upvoted more often and accepted as an answer - it's afaik the only library that is still being maintained.
4

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.

Comments

2

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...

Comments

1

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.

6 Comments

@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.
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?"
Sometimes it is nice to teach people to learn, not just reuse.
Yep sometimes, but generally, isn't it better reuse? I think that in that way you can spend your time in your own original stuff.
If that were the case, why on earth would he be writing a Java editor in the first place?
|
-4

http://www.neathighlighter.com/ is a good JavaScript highlighter

Comments