• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to get column position and row position from jtextarea

 
Anand Gajjar
Ranch Hand
Posts: 31
Java Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i want logic like notepad in which status bar updating with row position and column position.
any idea.....:-)
 
Rob Spoor
Sheriff
Posts: 20820
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
getCaretPosition() returns the absolute position. You can then use getLineOfOffset to get the line number. That's the row (0 based). The column is then quite easy - getLineStartOffset returns the position of the first character on the line. The difference between that and the caret position is the column.
 
Paul Clapham
Sheriff
Posts: 21882
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That position is called the "caret" in Swing. And you can add a CaretListener to a Swing text component to keep track of where the caret is located.

However that doesn't give you row and column locations; this is mainly (I think) because text components aren't normally designed to have rows and columns. They are normally designed to contain a continuous stream of text instead. Rows and columns can be changed by choosing a new font, or by changing the size of the component, or probably in other ways as well. So converting the caret location to row and column positions is going to be a difficult task.
 
Rob Camick
Ranch Hand
Posts: 2699
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Text Utilities provides a couple of methods for this.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!