title: How to use Caret to tell which line it is in from JTextPane? (Java)
body: <p>Problem:
  I have CaretListener and DocumentListener listening on a JTextPane.</p>
  
  <p>I need an algorithm that is able to tell which line is the caret at in a JTextPane, here's an illustrative example:</p>
  
  <p><img src="https://i126.photobucket.com/albums/p109/eXPeri3nc3/StackOverflow/caret1.png" alt="alt text"></p>
  
  <p>Result: 3rd line</p>
  
  <p><img src="https://i126.photobucket.com/albums/p109/eXPeri3nc3/StackOverflow/caret2.png" alt="alt text"></p>
  
  <p>Result: 2nd line</p>
  
  <p><img src="https://i126.photobucket.com/albums/p109/eXPeri3nc3/StackOverflow/caret3.png" alt="alt text"></p>
  
  <p>Result: 4th line</p>
  
  <p>and if the algorithm can tell which line the caret is in the JTextPane, it should be fairly easy to substring whatever that is in between the parentheses as the picture (caret is at character <code>m</code> of <code>metadata</code>):</p>
  
  <p><img src="https://i126.photobucket.com/albums/p109/eXPeri3nc3/StackOverflow/caret4.png" alt="alt text"></p>
  
  <p>-- </p>
  
  <p>This is how I divide the entire text that I retrieved from the JTextPane into sentences:</p>
  
  <pre><code>String[] lines = textPane.getText().split("\r?\n|\r", -1);
  </code></pre>
  
  <p>The sentences in the <code>textPane</code> is separated with \n.</p>
  
  <p>Problem is, <strong>how can I manipulate the caret to let me know at which position and which line</strong> it is in? I know the dot of the caret says at which position it is, but <strong>I can't tell which line it is at</strong>. Assuming if I know which line the caret is, then I can just do <code>lines[&lt;line number&gt;]</code> and manipulate the string from there.</p>
  
  <p>In Short: How do I use <em>CaretListener and/or DocumentListener</em> to know <strong>which line the caret is currently at</strong>, and <strong>retrieve the line</strong> for further string manipulation? Please help. Thanks.</p>
  
  <p>Do let me know if further clarification is needed. Thanks for your time.</p>