Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > display line number in JTextPane

Reply
Thread Tools

display line number in JTextPane

 
 
Fabrice
Guest
Posts: n/a
 
      03-03-2004
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
 
Reply With Quote
 
 
 
 
ak
Guest
Posts: n/a
 
      03-03-2004
> 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.


 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?Daniel_Sj=F6blom?=
Guest
Posts: n/a
 
      03-03-2004
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().getViewP osition()); //
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_CLOS E);
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

 
Reply With Quote
 
=?ISO-8859-1?Q?Daniel_Sj=F6blom?=
Guest
Posts: n/a
 
      03-03-2004
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().getViewP osition()); //
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

 
Reply With Quote
 
praitheesh praitheesh is offline
Junior Member
Join Date: Dec 2008
Posts: 3
 
      12-31-2008
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.

Quote:
Originally Posted by =?ISO-8859-1?Q?Daniel_Sj=F6blom?=
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().getViewP osition()); //
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
 
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 XML Formatted in JTextPane Michael Jung Java 5 09-17-2011 08:23 PM
JavaMail & JTextPane - display a message dawciobiel@gmail.com Java 1 02-13-2006 10:28 PM
Read a file line by line with a maximum number of characters per line Hugo Java 10 10-18-2004 11:42 AM
SPAN tag doesn't display properly in JTextPane Henry S. Thompson Java 0 05-01-2004 09:52 PM
JTextPane with line numbers down the side Alan Williams Java 2 12-16-2003 09:14 AM



Advertisments