• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Finding current line number - textarea

 
Ranch Hand
Posts: 276
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or is this a web app with a textarea tag?
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..!
 
Sheriff
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about getLineOfOffset?

Translates an offset into the components text to a line number.

 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to Swing/etc. forum.
 
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
These Text Utilities should help.
 
Rob Spoor
Sheriff
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
This tiny ad is wafer thin:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic