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

Finding current line number - textarea

 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is it possible to find the line number in which the user places the caret?
i've been thinking about this for a long time...but couldnt find a convincing solution.
does anyone here know how to do this??
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its Java Swing question and shouldn't be posted here, some one will move this for you in Swing JFC section,
Between, Has JTextArea API got any method for retuning the row column position ?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or is this a web app with a textarea tag?
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops..sorry for choosing the wrong place to post such a question..

Its javax.swing.TextArea i'm talking about..
the api available can jus help me to find the current caret position but not the linee number..!
 
Rob Spoor
Sheriff
Pie
Posts: 20510
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about getLineOfOffset?
Translates an offset into the components text to a line number.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to Swing/etc. forum.
 
Fred Hamilton
Ranch Hand
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, in addition to what others have suggested, I suppose if you know the width in characters of your text area (I believe that is not difficult to find out) then knowing the caret position you can apply a little arithmetic to determine the row#
 
Rob Spoor
Sheriff
Pie
Posts: 20510
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Hamilton wrote:if you know the width in characters of your text area (I believe that is not difficult to find out)

Unfortunately, for most fonts there is no uniform width for characters. Therefore, a line with many w's will contain fewer characters than a line with many i's.
 
Fred Hamilton
Ranch Hand
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:
Fred Hamilton wrote:if you know the width in characters of your text area (I believe that is not difficult to find out)

Unfortunately, for most fonts there is no uniform width for characters. Therefore, a line with many w's will contain fewer characters than a line with many i's.


OK, I was thinking of JTextArea, and if what you say is true, then I misunderstand the meaning of the getColumns() method of JTextArea. Or the fact that that one of the JTextArea constructors allows you to specify the number of rows and columns.

I can see you logic applying to JTextPane, but not JTextArea. What am I missing?

regards.
 
Rob Spoor
Sheriff
Pie
Posts: 20510
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought about what you said so I tested it. JTextArea uses the Dialog font on my PC by default, and that is not a mono spaced font. I've tried it a bit, and the number of columns fits exactly only if there are only W's on the line - capital W is the widest character there is. Lowercase w and especially i will fit more than the number of columns specified until scrollbars appear.
 
Fred Hamilton
Ranch Hand
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
noted. I also looked into it, and while the API doc remarks about getColumns() didn't really deal with the issue, the remarks for getColumnWidth() did...

Gets column width. The meaning of what a column is can be considered a fairly weak notion for some fonts. This method is used to define the width of a column. By default this is defined to be the width of the character m for the font used. This method can be redefined to be some alternative amount.

So it's as you originally said, in which case I guess we'd have to say that getColumns is based on content, and therefore has limited usefulness , or at least USE WITH CAUTION.
 
Rob Camick
Ranch Hand
Posts: 2556
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
These Text Utilities should help.
 
Rob Spoor
Sheriff
Pie
Posts: 20510
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I repeat:
Rob Prime wrote:How about getLineOfOffset?
Translates an offset into the components text to a line number.

I've just tried it, and it shows me the current line number (starting at 0 of course):
 
Fred Hamilton
Ranch Hand
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:These Text Utilities should help.


Yeah, they'll come in handy.

And your remark Also, the getColumnAtCaret() method only makes sense when using a Monospaced font. nicely articulates what I was thinking with getColumns() of JTextArea.

learn something every day.

p.s. indeed getLineOfOffsets answers the original question. I imagine behind the scenes it's counting pixels or something, give that columns don't mean much
 
Rob Spoor
Sheriff
Pie
Posts: 20510
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not quite. In fact, either Rob has copied the code or he's just very good at reading the API.

Rob's implementation of getLineAtCaret:
Sun's implementation of getLineOfOffset:
So apart from the explicit checking, the code is actually the same (although Rob's lines are 1-based and Sun's are 0-based).
 
Rob Camick
Ranch Hand
Posts: 2556
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Hamilton wrote:And your remark Also, the getColumnAtCaret() method only makes sense when using a Monospaced font. nicely articulates what I was thinking with getColumns() of JTextArea.


One way to determine the column is to simply get the start offset from the Element and subtract that from the caret position. Of course that won't work when you have tab characters. For a JTextArea you could query the tab character size and then iterate through all the characters in the string to find the number of tabs. But that solution won't work for a JTextPane which uses pixels for tab spacing. Since I wrote the code for a simple editor I was building at the time, I decided to go with the "view approach" to calculate the column based on a monospaced font.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic