0

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?

mundomug
  • 132
  • 2
  • 11

1 Answers1

0

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.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Thank you for your help. I did what you suggested and it works like a charm! Anyway, could you please tell me why I shouldn't use row numbers as part of the Document? Thanks! – mundomug Jan 29 '14 at 19:45
  • @mundomug, because they are not part of the text. When you use the getText() method, I'm sure you don't want to see the numbers. – camickr Jan 29 '14 at 20:53