0

I have done a GUI in Java with a JTextArea. It is filled with the content of a file. When I select words with the mouse on the textarea, a new frame pops up on which I do some operations on the selected words. To do these operations, I need to know the line number of the selected text...

Does someone know how to get the line number?

(I look to some methods on the classes JTextArea and MouseListener, but i dont know how to do that...)

Thanks ;)

mKorbel
  • 109,525
  • 20
  • 134
  • 319

1 Answers1

1

Check out the Text Utilities. The getLineAtCaret() method is close to what you need. It uses the offset of the caret to get the line number. In you case you will need to use the start offset of the selected text.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Thanks for your answer. I've tried that on my code, on the getLineAtCaret() method works on its own, no need to use the start offset of the selected text as you suggest? – user3371135 Mar 03 '14 at 13:23
  • @user3371135, yes you are right the caret must always be either at te start/end of the selected text. I was thinking or "highlighted" text which could be on a different line than the caret. – camickr Mar 03 '14 at 16:05