Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1286
  • Last Modified:

Get line number from caret position

I’m using JEditorPane and I want to know how get  line number from caret position.
0
csound
Asked:
csound
  • 2
  • 2
2 Solutions
 
edwardiiiCommented:
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":

     jEditorPane1.getCaretPosition()
0
 
sciuriwareCommented:
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.
;JOOP!
0
 
csoundAuthor Commented:
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) {
            super(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 + "]" +
                            nl);
                } 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);
            }
        }
    }
0
 
sciuriwareCommented:
int getLineNumber(String text, int caret)
{
int lineNumber = 1;
int pos = 0;

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

Or such.
;JOOP!
0
 
edwardiiiCommented:
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?

     
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now