This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan 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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTextarea go to line" Watch "JTextarea go to line" New topic
Author

JTextarea go to line

Stefan Cosma
Greenhorn

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
Greenhorn

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
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

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


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Stefan Cosma
Greenhorn

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 agree. Here's the link: http://aspose.com/file-tools
 
subject: JTextarea go to line
 
Similar Threads
command execution
getting the first four characters from a line
reading content of files line by line
string getting converted to ASCII
Problem writing a string into a file