Moving around in a j scroll pane.
I have a JTextArea inside a JScrollPane and I'm trying to get the scroll pane to goto a certain line number that I want by using:
TA.setCaretPosition(10*linenumber);
the linenumber is the line number the user enters and then the 10 is just the pixel it will go down because the interval I counted between each line is about 10. But it doesn't seem to move at all, what am I doing wrong?
[407 byte] By [
jpeanuta] at [2007-11-27 11:46:37]

# 1
I guess it doesn't work like that.
What you are doing is moving the caret to a certain position in the document. The setCaretPosition method expects the CHARACTER position in the document to which to move, and has nothing to do with pixels...
But this won't be moving the scroll pane at all. I think what you are looking for is the TA.scrollRectToVisible(Rectangle) method. You pass a Rectangle on the textarea that you want to be made visible, and the call is repassed to the parents until a JViewPort is found, which is then scrolled.
To calculate this rectangle you could use TA.getRows() and TA.getSize().height, for example, to determine how much space each row is really occupying.
Best regards,
Marcos.
# 2
First of all, learn the standard Java coding conventions regarding variable names:
"linenumber" should be "lineNumber"
"TA" is completely wrong since its not descriptive and upper cased words imply static final variables. So you should be using at least something like "textArea".
Check out this posting:
http://forum.java.sun.com/thread.jspa?forumID=57&threadID=609993