Question

I'm using JTextPane in a project where I'm supposed to display row numbers and color some keywords. For displaying row numbers, I'm using 'setEditorKit' and for coloring the text, I'm overriding insertString() and removeString() methods of DefaultStyledDocument and then using 'JTextPane.setDocument(DefaultStyledDocument_object)'.

The problem I'm facing is that if I add EditorKit to my JTextPane, the coloring part doesn't work. And if I add the the DefaultStyledDocument after setting the EditorKit, the insertString method shifts the caret to the 'zero caret position', hence messing up the UI.

Here's an image of the functionality I'm trying to achive.

What I want
This is what I want to my JTextPane to look like.

Mess
This is what it currently looks like

How do I use both 'setEditorKit' and 'DefaultStyledDocument' in my application?

Was it helpful?

Solution

Don't use row numbers as part of the Document. Instead you should create a component that displays row numbers and use the component as a row header for your scroll pane.

See Text Component Line Number for an example of this approach.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top