JTextPane with line numbers down the side

A

Alan Williams

I would like to create a text editor that displays its line numbers on
the left hand side. The line numbers will need to scroll with the
document and handle lines being added or removed.

I'm trying the approach of using one JTextPane within a JScrollPane to
handle the main document. A second non-editable JTextPane is being
used as the RowHeaderView of the JScrollPane. It starts with the same
number of lines as the main document and simply has the text 1, 2, 3
etc...

This works quite well and the line numbers scroll with the main
document. But if you add new rows then I'm having trouble updating the
list of line numbers. When I insert an extra line number (using
setText) at the end the whole list scrolls to the bottom, so it's out
of synch with the main document. I've tried to scroll the line numbers
to be aligned in the correct position immediately afterwards (using
scrollRectToVisible) but this isn't working. I need some way to force
it to re-align itself.

Can anyone help, or have a different approach altogether that I can
use?

Thanks.

Alan
 
M

Matthew Zimmer

Alan said:
I would like to create a text editor that displays its line numbers on
the left hand side. The line numbers will need to scroll with the
document and handle lines being added or removed.

I'm trying the approach of using one JTextPane within a JScrollPane to
handle the main document. A second non-editable JTextPane is being
used as the RowHeaderView of the JScrollPane. It starts with the same
number of lines as the main document and simply has the text 1, 2, 3
etc...

This works quite well and the line numbers scroll with the main
document. But if you add new rows then I'm having trouble updating the
list of line numbers. When I insert an extra line number (using
setText) at the end the whole list scrolls to the bottom, so it's out
of synch with the main document. I've tried to scroll the line numbers
to be aligned in the correct position immediately afterwards (using
scrollRectToVisible) but this isn't working. I need some way to force
it to re-align itself.

Can anyone help, or have a different approach altogether that I can
use?

Thanks.

Alan

In order to truly get a scroll pane to scroll properly, don't use
scrollRectToVisible...that method rarely seems to work properly for me.
Instead, move the caret in your "number" JTextPane to the last
character. This can be done as such:

Document doc = yourNumberPane.getDocument();
yourNumberPane.setCaretPosition(doc.getLength());

This forces an automatic scrolling to the end that actually works.

HTH
Matthew
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top