JTextPane with line numbers down the side

Discussion in 'Java' started by Alan Williams, Dec 12, 2003.

  1. 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
     
    Alan Williams, Dec 12, 2003
    #1
    1. Advertising

  2. 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
     
    Matthew Zimmer, Dec 12, 2003
    #2
    1. Advertising

  3. Matthew,
    It certainly does make it scroll.

    Thanks.


    Alan
     
    Alan Williams, Dec 16, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rj

    JTextPane columnar data

    rj, Sep 1, 2003, in forum: Java
    Replies:
    1
    Views:
    505
    Sandip Chitale
    Sep 1, 2003
  2. Adam
    Replies:
    0
    Views:
    458
  3. Sudarshan Raghavan

    JTextPane memory leak

    Sudarshan Raghavan, Oct 28, 2003, in forum: Java
    Replies:
    1
    Views:
    1,203
    Robert Olofsson
    Oct 29, 2003
  4. Fabrice
    Replies:
    4
    Views:
    17,520
    praitheesh
    Dec 31, 2008
  5. Henry S. Thompson

    SPAN tag doesn't display properly in JTextPane

    Henry S. Thompson, May 1, 2004, in forum: Java
    Replies:
    0
    Views:
    529
    Henry S. Thompson
    May 1, 2004
Loading...

Share This Page