Get line number from caret position

Posted on 2005-04-22
Last Modified: 2007-12-19
I’m using JEditorPane and I want to know how get  line number from caret position.
Question by:csound
    LVL 10

    Expert Comment

    Hi, csound.

    If the caret is at the first position (e.g. top) of the jEditorPane, then the following code returns "0".  If you move the caret/cursor down one line, the code returns "1":

    LVL 24

    Expert Comment

    You must count the number of newlines in the result from JEditorPane.getText().
    Then add 1 if the last character is not a newline, so
    your 1st line numbers 1.

    Author Comment

    Hmmm may be my question was not clear enough.
    I have the following code which displays caret position every time the caret gets updated.
    But now, I’m trying to modify that so that the JLabel displays in which line the caret is located.

        protected class CaretListenerLabel extends JLabel implements CaretListener {
            public CaretListenerLabel (String label) {
            public void caretUpdate(CaretEvent e) {
                //Get the location in the text
                int dot = e.getDot();
                int mark = e.getMark();
                if (dot == mark) {  // no selection
                    try {
                        Rectangle caretCoords = editor.modelToView(dot);
                        //Convert it to view coordinates
                        setText("caret: text position: " + dot +
                                ", view location = [" +
                                caretCoords.x + ", " + caretCoords.y + "]" +
                    } catch (BadLocationException ble) {
                        setText("caret: text position: " + dot + nl);
                } else if (dot < mark) {
                    setText("selection from: " + dot + " to " + mark + nl);
                } else {
                    setText("selection from: " + mark + " to " + dot + nl);
    LVL 24

    Accepted Solution

    int getLineNumber(String text, int caret)
    int lineNumber = 1;
    int pos = 0;

       while((pos = text.indexOf(nl, pos)) > 0)
       int lineNumber = getLineNumber(textarea.getText(), textarea.getCaretposition());

    Or such.
    LVL 10

    Assisted Solution

    Using Netbeans, I created a new JFrame class, dropped a JEditorPane and JLabel onto the JFrame, and populated the JEditorPane's "Caret/CaretUpdate" event with:

          private void jEditorPane1CaretUpdate(javax.swing.event.CaretEvent evt)
              jLabel1.setText("Current caret position is: " + (jEditorPane1.getCaretPosition() + 1));

    This keeps the JLabel's text painted with the line number the caret is located in, presuming we consider the top line
    as line "1".  Am I understanding the functionality you're looking for correctly?


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    If you have upgraded to Java2 update 10 on a Microsoft Windows client, you may have discovered that your Java application does not work as it did before.  For example, the colors of your Java2D graphic may be all wrong for no apparent reason. Aft…
    Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
    Video by: Michael
    Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
    This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now