Here you go:
...
JPanel textPanel = new TextPanel(new JTextPane());
JScrollPane scroller = new JScrollPane(textPanel);
...
class TextPanel extends JPanel {
JTextPane textPane;
public TextPanel(JTextPane tp) {
textPane = tp;
setLayout(new BorderLayout());
// create a left margin to contain the line numbers
setBorder(BorderFactory.createEmptyBorder(0,18,0,0));
add(textPane, BorderLayout.CENTER);
}
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.black);
// compute information to help draw the line numbers
int row_height = getFontMetrics(textPane.getFont()).getHeight();
int height = getHeight();
// top margin depends on your font -- may need to adjust a little
int top_margin = text.getMargin().top + 14;
int left_margin = 2;
int line=1;
// draw the line numbers
for(int y=top_margin; y<height; y+=row_height) {
g.drawString("" + line++, left_margin, y);
}
}
}
~Brett
> -----Original Message-----
> From: Panagiotis Plevrakis [mailto:[EMAIL PROTECTED]]
> Sent: Monday, July 01, 2002 6:36 PM
> To: JDJList
> Subject: [jdjlist] Re: Line numbers
>
>
> That sounds a good idea. Could you give me some code help?
>
>
>
> >From: "Borchardt, Brett" <[EMAIL PROTECTED]>
> >Reply-To: "JDJList" <[EMAIL PROTECTED]>
> >To: "JDJList" <[EMAIL PROTECTED]>
> >Subject: [jdjlist] Re: Line numbers
> >Date: Mon, 1 Jul 2002 17:52:18 -0500
> >
> >I recently did something like this. The approach I took was to add a
> >JTextPane to a JPanel with a wide left margin and then
> override the paint()
> >method in the JPanel to draw the line numbers in the margin using
> >Graphics.drawString(). You can then add the panel to a
> JScrollPane to get
> >a
> >scrolling text pane with line numbers.
> >
> >Brett
> >
> > > -----Original Message-----
> > > From: Panagiotis Plevrakis [mailto:[EMAIL PROTECTED]]
> > > Sent: Monday, July 01, 2002 5:32 PM
> > > To: JDJList
> > > Subject: [jdjlist] Line numbers
> > >
> > >
> > > I'm trying to implement visible line numbers in a code
> > > editor. I don't want
> > > the numbers to be part of the code. So, my first approach is
> > > to create a
> > > JTextArea to the left of the main editor area that will
> > > listen to the text
> > > length changes and display the appropriate sequence of line
> > > numbers. If
> > > someone has done something similar or has any ideas to give
> > > me, I would
> > > appreciate it.
> > >
> > > Panos
> > >
> > > _________________________________________________________________
> > > Send and receive Hotmail on your mobile device:
http://mobile.msn.com
> >
> >
> > To change your membership options, refer to:
> > http://www.sys-con.com/java/list.cfm
> >
>
>To change your membership options, refer to:
>http://www.sys-con.com/java/list.cfm
_________________________________________________________________
Join the world's largest e-mail service with MSN Hotmail.
http://www.hotmail.com
To change your membership options, refer to:
http://www.sys-con.com/java/list.cfm
To change your membership options, refer to:
http://www.sys-con.com/java/list.cfm