display line number in JTextPane

Discussion in 'Java' started by Fabrice, Mar 3, 2004.

  1. Fabrice

    Fabrice Guest

    Hi !

    I would like to know if it is possible to display the line number in a
    JTextPane ? I design an editor and I'd like to offer the same feature
    than a programer's notepad that show the number of EACH line at the
    left side of the text.

    regards,
    Fabrice
     
    Fabrice, Mar 3, 2004
    #1
    1. Advertising

  2. Fabrice

    ak Guest

    > I would like to know if it is possible to display the line number in a
    > JTextPane ? I design an editor and I'd like to offer the same feature
    > than a programer's notepad that show the number of EACH line at the
    > left side of the text.


    usually line numbers displayed in pane left from text panel.

    ____________

    http://reader.imagero.com the best java image reader.
     
    ak, Mar 3, 2004
    #2
    1. Advertising

  3. Fabrice wrote:

    > Hi !
    >
    > I would like to know if it is possible to display the line number in a
    > JTextPane ? I design an editor and I'd like to offer the same feature
    > than a programer's notepad that show the number of EACH line at the
    > left side of the text.
    >


    It is fairly easy, but requires knowledge of swing idiosynchrasies. Here
    is some code to get you started (probably munged by posting, try loading
    it up in a beautifying editor):

    package com.dsjoblom.junk;

    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Point;

    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextPane;
    import javax.swing.text.Document;

    /**
    * A class illustrating running line number count on JTextPane. Nothing
    is painted on the pane itself,
    * but a separate JPanel handles painting the line numbers.<br>
    *
    * @author Daniel Sjöblom<br>
    * Created on Mar 3, 2004<br>
    * Copyright (c) 2004<br>
    * @version 1.0<br>
    */
    public class LineNr extends JPanel
    {
    // for this simple experiment, we keep the pane + scrollpane as members.
    JTextPane pane;
    JScrollPane scrollPane;

    public LineNr()
    {
    super();
    setMinimumSize(new Dimension(30, 30));
    setPreferredSize(new Dimension(30, 30));
    setMinimumSize(new Dimension(30, 30));
    pane = new JTextPane() // we need to override paint so that the
    linenumbers stay in sync
    {
    public void paint(Graphics g)
    {
    super.paint(g);
    LineNr.this.repaint();
    }
    };
    scrollPane = new JScrollPane(pane);

    }
    public void paint(Graphics g)
    {
    super.paint(g);

    // We need to properly convert the points to match the viewport
    // Read docs for viewport
    int start =
    pane.viewToModel(scrollPane.getViewport().getViewPosition()); //
    starting pos in document
    int end =
    pane.viewToModel(
    new Point(
    scrollPane.getViewport().getViewPosition().x + pane.getWidth(),
    scrollPane.getViewport().getViewPosition().y + pane.getHeight()));
    // end pos in doc

    // translate offsets to lines
    Document doc = pane.getDocument();
    int startline = doc.getDefaultRootElement().getElementIndex(start);
    int endline = doc.getDefaultRootElement().getElementIndex(end);

    int fontHeight = g.getFontMetrics(pane.getFont()).getHeight(); // font
    height

    for (int line = startline, y = 0; line <= endline; line++, y +=
    fontHeight)
    {
    g.drawString(Integer.toString(line), 0, y);
    }

    }

    // test main
    public static void main(String[] args)
    {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(new BorderLayout());
    final LineNr nr = new LineNr();
    frame.getContentPane().add(nr, BorderLayout.WEST);
    frame.getContentPane().add(nr.scrollPane, BorderLayout.CENTER);
    frame.pack();
    frame.setSize(new Dimension(400, 400));
    frame.show();
    }
    }



    --
    Daniel Sjöblom
    Remove _NOSPAM to reply by mail
     
    =?ISO-8859-1?Q?Daniel_Sj=F6blom?=, Mar 3, 2004
    #3
  4. Daniel Sjöblom wrote:

    > Fabrice wrote:
    >
    >> Hi !
    >>
    >> I would like to know if it is possible to display the line number in a
    >> JTextPane ? I design an editor and I'd like to offer the same feature
    >> than a programer's notepad that show the number of EACH line at the
    >> left side of the text.
    >>

    >
    > It is fairly easy, but requires knowledge of swing idiosynchrasies. Here
    > is some code to get you started (probably munged by posting, try loading
    > it up in a beautifying editor):

    <snip code>

    I noticed there were some bugs in the code, swap this for the original
    paint method:

    public void paint(Graphics g)
    {
    super.paint(g);

    // We need to properly convert the points to match the viewport
    // Read docs for viewport
    int start =
    pane.viewToModel(scrollPane.getViewport().getViewPosition()); //
    starting pos in document
    int end =
    pane.viewToModel(
    new Point(
    scrollPane.getViewport().getViewPosition().x + pane.getWidth(),
    scrollPane.getViewport().getViewPosition().y + pane.getHeight()));
    // end pos in doc

    // translate offsets to lines
    Document doc = pane.getDocument();
    int startline = doc.getDefaultRootElement().getElementIndex(start) + 1;
    int endline = doc.getDefaultRootElement().getElementIndex(end) + 1;

    int fontHeight = g.getFontMetrics(pane.getFont()).getHeight();
    int fontDesc = g.getFontMetrics(pane.getFont()).getDescent();
    int starting_y = -1;

    try
    {
    starting_y = pane.modelToView(start).y -
    scrollPane.getViewport().getViewPosition().y + fontHeight - fontDesc;
    }
    catch (BadLocationException e1)
    {
    e1.printStackTrace();
    }

    for (int line = startline, y = starting_y; line <= endline; y +=
    fontHeight, line++)
    {
    g.drawString(Integer.toString(line), 0, y);
    }

    }

    --
    Daniel Sjöblom
    Remove _NOSPAM to reply by mail
     
    =?ISO-8859-1?Q?Daniel_Sj=F6blom?=, Mar 3, 2004
    #4
  5. Fabrice

    praitheesh

    Joined:
    Dec 31, 2008
    Messages:
    3
    get Line number

    is any way to get the selected line number form jTextArea ?as well as when we give the line number it want to select the entire line!!!
    Can any one help to find this.

     
    praitheesh, Dec 31, 2008
    #5
    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. Alan Williams
    Replies:
    2
    Views:
    4,892
    Alan Williams
    Dec 16, 2003
  2. Henry S. Thompson

    SPAN tag doesn't display properly in JTextPane

    Henry S. Thompson, May 1, 2004, in forum: Java
    Replies:
    0
    Views:
    534
    Henry S. Thompson
    May 1, 2004
  3. Hugo
    Replies:
    10
    Views:
    1,385
    Matt Humphrey
    Oct 18, 2004
  4. Replies:
    1
    Views:
    774
  5. Michael Jung

    Display XML Formatted in JTextPane

    Michael Jung, Apr 7, 2008, in forum: Java
    Replies:
    5
    Views:
    6,463
    negrini
    Sep 17, 2011
Loading...

Share This Page