Hi, i have a jtextpane and a JComponet that displays line numbers. Does anyone know how i can get the startline and end line of the text that is currently in view on the JTextPane?


I can give you an idea of how to do it. For the starting line number, determine how much the view has already been scrolled and divide it by the amount of pixels taken up by a line number.

If line numbers increment every 16 pixels, then:

starting line # = X/16

If a the number isn't a whole number, (like you get 4.25) then it means line 4 is only partially visible.

You can also find the last line number the same way by adding the amount already scrolled above the viewport and the height of the overall viewport.

ending line # = (X + viewport.height)/16


Thanks for the reply it sounds logical.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.