Thanks everybody. I just got a reply for the same from a friend.
He says
Hi Lionel!
It is easiest to do this in a JTextPane. Create a new DefaultStyledDocument, and then create a JTextPane around this, and add the JTextPane to your frame/applet/whatever.
DefaultStyledDocument doc = new DefaultStyledDocument();
myFrame.getContentPane().add(new JTextPane(doc));
Next, you will need to make an AttributeSet, which, for instance, sets the foreground colour to red. The concept of a foreground colour setting is represented by the object StyleConstants.ColorConstants.Foreground, and to specify what this should be set to, use the java.awt.Color constants.
SimpleAttributeSet attribs = new SimpleAttributeSet();
attribs.addAttribute(StyleConstants.ColorConstants.Foreground,Color.red);
This would define an AttributeSet which would set the text colour to red. To apply this to part of the document, we could use (for instance):
doc.setCharacterAttributes(4,3,attribs,true);
(the parameters are offset into document, number of chars over which to apply the style, the AttributeSet, and whether to replace the existing style).
To use this for syntax highlighting, you will need to implement the DocumentListener interface, and respond to changes to the document by making appropriate highlights.
If you are thinking of writing a complex editor, you should consider having a look at JEditorPane. As for your answer, it works.
So thanks, anyway. Hope this is a learning for both of us.