This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes JTextarea go to line Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTextarea go to line" Watch "JTextarea go to line" New topic

JTextarea go to line

Stefan Cosma

Joined: Mar 30, 2012
Posts: 13

Hello guys,

I have a problem.

How can I jump to a specific line (using the line specified in a JTextfield) in a JTextarea ?

This is what I have so far, but it doesn't seem to work.

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
line numbers are 0-based, and if you have line wrap set, it won't count as additional lines

where '3' is the 4th line
Stefan Cosma

Joined: Mar 30, 2012
Posts: 13

I finally got it working, thanks Michael for your help.

Maybe somebody will need the following code. This is how I did it.

Rob Spoor

Joined: Oct 27, 2005
Posts: 20077

Why do you use index-2? Shouldn't that be index-1 instead?

How To Ask Questions How To Answer Questions
Stefan Cosma

Joined: Mar 30, 2012
Posts: 13

Well believe it or not, index-2 works. Initially I used index-1 but it didn't set the caret to the correct line.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: JTextarea go to line
It's not a secret anymore!