2

Possible Duplicate:
Rows in JTextArea

In JTextArea, operations such as select, highlight and so on seem all to depend on offset from beginning of text. In an app that displays line-oriented text, I need to select, highlight (based on info from elsewhere, not caret) based on row and column.

Is there some functionality built-in, or in some helper class, to get offset from row,col? I realize I could maintain separate data on line-start offsets etc and calculate row,col-->offset, but surely JTextArea (or its model) already knows this in order to display the text, so I'm persuaded there must already be a way to do this.

I did see examples that use something like this one, using textarea.viewToModel(new Point(x,y));, where x and y were purportedly row,col, but so far as I can see, x, and y are pixel coordinates, not row,col... so not sure what to make of that.

Clues? Thanks!

Edited: So the question has been closed under the impression of five commenters that this is a duplicate of other questions, which it is not. I did not ask about how to convert offset to row, col, nor about how to convert screen pixel coordinates to offset, which are the subjects covered in the other articles.

In case someone else stumbles in here looking for the answer to what I actually did ask, I've now discovered that it's as follows.

JTextArea has the functionality I expected to find, but evidently overlooked on earlier browsing: getLineStartOffset(int line) which will give the offset-from-start-of-text for a particular line (row) of text. To this one can easily add the char-posn-in-line, and thus arrive at the offset of a particular character.

Community
  • 1
  • 1
gwideman
  • 2,705
  • 1
  • 24
  • 43
  • 1
    See also the utilities mentioned in this [Q&A](http://stackoverflow.com/q/13130486/230513). – trashgod Nov 20 '12 at 03:11
  • Well, evidently this question has been closed, I believe due to misunderstanding that I'm asking the reverse of what I was actually asking. I will post again and try to be even clearer. – gwideman Nov 20 '12 at 10:08
  • ...guess I'll skip re-asking the question since I've now answered it -- answer editing into the question for posterity. – gwideman Nov 20 '12 at 10:55
  • +1 for improving the question. – trashgod Nov 20 '12 at 14:55

2 Answers2

2

A textArea contains a stream of text. There's no magic method that will locate a row/column in your text model, since that can be ambiguous if your text contains variable-width characters or different font sizes. You must maintain the data necessary to map from your idea of what a particular (row,column) means with regard to your data.

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
  • 1
    FWIW, I use "column" to mean "character position within row". The meaning of row 10, character 3 is not ambiguous, and does not depend on character width, nor font size. To be sure, wrapping would be affected by char width and font size, but (a) I am dealing with non wrapped text, and (b) even when wrapped, the meaning of row 10, char 3 is still unambiguous. – gwideman Nov 20 '12 at 10:07
2

That's simple example of rectangular fragment selection http://java-sl.com/tip_vertical_selection.html

You can use javax.swing.text.Utilities.getRowStart()/getRowEnd() methods. First find start row offset for the row number. Then just add col number to get the offset.

StanislavL
  • 56,971
  • 9
  • 68
  • 98
  • 1
    Counter to expectations, getRowStart does not find the start offset from row index. However, as it turns out, JTextArea.getLineStartOffset() does. – gwideman Nov 20 '12 at 10:58
  • @gwideman finally answered my question, I'd been digging for an hour, THANKS – Blake Miller Dec 12 '13 at 08:59
  • Huh... I just found that getLineStartOffset() in particular _doesn't_ find the start offset of a row, but rather, it's returning (for me) the start offset of a _line_... which is not the same thing, if the line wraps. – Hakanai Jul 27 '15 at 04:11