Abdullah Tellioglu
Abdullah Tellioglu

Reputation: 1464

JTextPane scroll to specific row

I am trying to scroll JTextPane to specific line. For doing that, i put my text pane into the JScrollPane as JViewport. I determine the x and y coordinates of the line with :

JViewport port = (JViewport)getParent();
JScrollPane pane = (JScrollPane)port.getParent();
float calculated = (float)i/(float)lines.size() * 100;
pane.getVerticalScrollBar().setMaximum(100);
pane.getVerticalScrollBar().setValue((int)calculated);

With this code block, i can scroll the scrollbar but i can not scroll the context of text area. Only scrollbar moves, but displaying text seems the same as before. I need to display the line which is calculated.(By the way calculated variable is between 0-100)

Upvotes: 0

Views: 415

Answers (1)

camickr
camickr

Reputation: 324098

Check out the Text Utilities class.

You can use the gotoStartOfLine(...) method to position the caret at the beginning of a line. This will cause the text area to scroll if required.

Or maybe you can use the centerLineInScrollPane(...) method.

Upvotes: 2

Related Questions