Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > JTextPane with line numbers down the side

Reply
Thread Tools

JTextPane with line numbers down the side

 
 
Alan Williams
Guest
Posts: n/a
 
      12-12-2003
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
 
Reply With Quote
 
 
 
 
Matthew Zimmer
Guest
Posts: n/a
 
      12-12-2003


Alan Williams wrote:

> 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

 
Reply With Quote
 
 
 
 
Alan Williams
Guest
Posts: n/a
 
      12-16-2003
Matthew,
It certainly does make it scroll.

Thanks.


Alan
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
display line number in JTextPane Fabrice Java 4 12-31-2008 03:36 PM
Help: How do I change the type of text list, from up-down to side-by-side... Eric Computer Support 5 07-28-2005 10:44 PM
JTextPane memory leak Sudarshan Raghavan Java 1 10-29-2003 11:33 AM
JTextPane does not highlight when not in focus Adam Java 0 10-18-2003 08:19 PM
JTextPane columnar data rj Java 1 09-01-2003 05:18 PM



Advertisments