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.