Where can I find a good tutorial re: implementing syntax highlighting in a javax.swing.JEditorPane.
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Ted_Berg
Posted On:   Sunday, April 8, 2001 07:36 PM

I have followed a number of false starts, the most promising of which was at http://java.sun.com/products/jfc/tsc/articles/text/editor_kit/index.html .

This particular example is *very* tightly welded to the java language, and ( if memory serves ) requries some reworking to even compile on jdk1.3. I've got an implementation that uses a tokenizer created with javacc, but I'm running into a number of errors that I haven't been able to track down.

Re: Where can I find a good tutorial re: implementing syntax highlighting in a javax.swing.JEditorPane.

Posted By:   Sandip_Chitale  
Posted On:   Monday, April 9, 2001 12:40 PM

The following article explains how to use the Attributes and Styles in a DefaultStyledDocument which happens to be the document for JEditorPane by default.


http://java.sun.com/products/jfc/tsc/articles/text/attributes/


Basically you have to set the attibutes for a run of text using -


SimpleAttributeSet sas = new SimpleAttributeSet();
StyleConstants.setForeground((Color)foregroundColorTable.get(toke.kind));
StyleConstants.setBackground((Color)backgroundColorTable.get(toke.kind));
textPane.getStyledDocument().setCharacterAttributes
(token.offset, token.length, sas, true);

I would simplify it by catagorizing the tokens into say -


  • KEYWORD

  • TYPE

  • IDENTIFIER

  • STRING

  • NUMBER

  • FORMAL_COMMENT

  • COMMENT

instead of using token.kind.


In fact you can define styles using
javax.swing.text.Style per category.

About | Sitemap | Contact