*
The moose likes Swing / AWT / SWT and the fly likes Finding current line number - textarea Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Finding current line number - textarea" Watch "Finding current line number - textarea" New topic
Author

Finding current line number - textarea

Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

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??


OCPJP 6
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

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 ?


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Or is this a web app with a textarea tag?
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

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

Joined: Oct 27, 2005
Posts: 19649
    
  18

How about getLineOfOffset?
Translates an offset into the components text to a line number.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Moving to Swing/etc. forum.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
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

Joined: Oct 27, 2005
Posts: 19649
    
  18

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

Joined: May 13, 2009
Posts: 679
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

Joined: Oct 27, 2005
Posts: 19649
    
  18

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

Joined: May 13, 2009
Posts: 679
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

Joined: Jun 13, 2009
Posts: 2152
    
    7
These Text Utilities should help.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

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

Joined: May 13, 2009
Posts: 679
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

Joined: Oct 27, 2005
Posts: 19649
    
  18

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

Joined: Jun 13, 2009
Posts: 2152
    
    7
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.

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Finding current line number - textarea
 
Similar Threads
exceptions
why i'm getting nullpointer exception here?
Difficulty in Updating database
Uploading a file?
How do I find a text file is Unix or Dos format via Java API?