tag:blogger.com,1999:blog-1282377935159788799.post-91868291018884143902007-12-28T09:24:00.000Z2007-12-28T10:08:52.289ZLine number in a JTextArea<a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://bp1.blogger.com/_L4tSSx7sAl4/R3TGWqSJptI/AAAAAAAAAD8/jj_e_NtgsJk/s1600-h/linenumber.png"><img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer;" src="http://bp1.blogger.com/_L4tSSx7sAl4/R3TGWqSJptI/AAAAAAAAAD8/jj_e_NtgsJk/s320/linenumber.png" alt="" id="BLOGGER_PHOTO_ID_5148958366601160402" border="0" /></a><br />The code below looks for the caret position in a <span style="font-family:courier new;">JTextArea </span>and returns the current line number.<br /><pre><span style="color: rgb(127, 0, 85);font-family:courier new;font-size:100%;" ><b>public </b></span><span style="color: rgb(127, 0, 85);font-family:courier new;font-size:100%;" ><b>int </b></span><span style="color: rgb(0, 0, 0);font-family:courier new;font-size:100%;" >getCurrentLineNumber</span><span style="color: rgb(0, 0, 0);font-family:courier new;font-size:100%;" >() {</span><span style="font-size:100%;"><br /></span><span style="color: rgb(127, 0, 85);font-family:courier new;font-size:100%;" ><b> int </b></span><span style="color: rgb(0, 0, 0);font-family:courier new;font-size:100%;" >line;</span><span style="font-size:100%;"><br /></span><span style="color: rgb(127, 0, 85);font-family:courier new;font-size:100%;" ><b> int </b></span><span style="color: rgb(0, 0, 0);font-family:courier new;font-size:100%;" >caretPosition = editorTextArea.getCaretPosition</span><span style="color: rgb(0, 0, 0);font-family:courier new;font-size:100%;" >()</span><span style="color: rgb(0, 0, 0);font-family:courier new;font-size:100%;" >;</span><span style="font-size:100%;"><br /></span><span style="color: rgb(0, 0, 0);font-family:courier new;font-size:100%;" > Element root =<br /> editorTextArea.getDocument</span><span style="color: rgb(0, 0, 0);font-family:courier new;font-size:100%;" >()</span><span style="color: rgb(0, 0, 0);font-family:courier new;font-size:100%;" >.getDefaultRootElement</span><span style="color: rgb(0, 0, 0);font-family:courier new;font-size:100%;" >()</span><span style="color: rgb(0, 0, 0);font-family:courier new;font-size:100%;" >;</span><span style="font-size:100%;"><br /></span><span style="color: rgb(0, 0, 0);font-family:courier new;font-size:100%;" > line = root.getElementIndex</span><span style="color: rgb(0, 0, 0);font-family:courier new;font-size:100%;" >(</span><span style="color: rgb(0, 0, 0);font-family:courier new;font-size:100%;" >caretPosition</span><span style="color: rgb(0, 0, 0);font-family:courier new;font-size:100%;" >) </span><span style="color: rgb(0, 0, 0);font-family:courier new;font-size:100%;" >+ </span><span style="color: rgb(153, 0, 0);font-family:courier new;font-size:100%;" >1</span><span style="color: rgb(0, 0, 0);font-family:courier new;font-size:100%;" >;</span><span style="font-size:100%;"><br /></span><span style="color: rgb(127, 0, 85);font-family:courier new;font-size:100%;" ><b> return </b></span><span style="color: rgb(0, 0, 0);font-family:courier new;font-size:100%;" >line;</span><span style="font-size:100%;"><br /></span><span style="color: rgb(0, 0, 0);font-family:courier new;font-size:100%;" >}</span><span style="font-size:85%;"><br /></span><br /></pre>The method can be used by a <span style="font-family:courier new;">CaretListener</span> implementation so that every time we move the caret inside the JTextArea we get an update on the current line number.Christoshttp://www.blogger.com/profile/11903246349533401198noreply@blogger.com