Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTextarea go to line

 
Stefan Cosma
Greenhorn
Posts: 13
Java Opera Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
Java Opera Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20510
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you use index-2? Shouldn't that be index-1 instead?
 
Stefan Cosma
Greenhorn
Posts: 13
Java Opera Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well believe it or not, index-2 works. Initially I used index-1 but it didn't set the caret to the correct line.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic