1

I can get row number and column number from Jtextarea by using,

   int caretpos = textArea.getCaretPosition();
   int row = textArea.getLineOfOffset(caretpos);

   int column = caretpos - textArea.getLineStartOffset(row);

How can i get the caret position from row number and column number?

FirmView
  • 3,130
  • 8
  • 34
  • 50
  • 2
    I guess you are looking for this line `int startOffset = tarea.viewToModel(new Point(x, y));`. Your **row and column** variables can act as **x and y** respectively, in the example that proceeds. For more info have a look at this [example](http://stackoverflow.com/a/10463120/1057230) – nIcE cOw Sep 08 '12 at 16:31
  • @Gagandeep Bali please I'm sure that you know that better, then this comment should be nice answer :-) – mKorbel Sep 08 '12 at 19:35
  • @mKorbel : I just realized, though the example can give answer to the end question that arises, as to how to insert a String at a specified location by the OP, in another thread, though how to calculate the `CaretPosition` is a bit tricky :-) – nIcE cOw Sep 09 '12 at 04:09

1 Answers1

2

for both your questions, answer could be deleted

  • Document is Model for JTextComponents

  • you can to determine int start / end for every row(s)

  • you can to determine int start / end for Caret by CaretListener

  • for listening of changes from view is there DocumentListener

  • for preventing / modifying of input is there DocumentFilter

mKorbel
  • 109,525
  • 20
  • 134
  • 319