colour coding text with JTextArea

Hello, im working on an IDE here and I’m trying to get the coloured text you see with alot of IDEs (NetBeans/Eclipse/VStudio). I was wondering if anyone knew how to do this because I’m kinda stumped. :-/

I thought maybe you could use the Graphics to draw everything like text, to colour and bold the font or whatever. But this way seemed impractical. Anyone have any ideas that would help?

Thanks in advance :wink:

This question comes up pretty frequently…

http://www.java-gaming.org/cgi-bin/JGNetForums/YaBB.cgi?board=Offtopic;action=display;num=1075215192

The JEditorPane can display attributed text. If your text is static, that is you don’t want to highlight stuff while input, it’s quite easy to find text ranges with regular expressions and apply attributes the editor’s styled document.

If you want to create simply syntax highlighting for a text editor without bothering with styles and attributes, you can create something like this:

http://groups.google.com/groups?selm=bnqs3t%24qed%2403%241%40news.t-online.com

The text is German, but the code shown should be useful on its own. The example automatically highlights all occurences of “der”, “die” or “das” of MyEditor’s text. It creates a new EditorKit (the working horse behind the JEditorPane) to create a new ViewFactory to create a new View object for the plain text. View objects are responsible to render text (or graphics) in the JEditorPane.