DevFaqFindCaretPositionInEditor

(Difference between revisions)
(How can I get the linenumber of the currently selected editor?)
(How can I get the linenumber of the currently selected editor?: Simplified example and added code to get the column)
Line 20: Line 20:
-
===How can I get the linenumber of the currently selected editor?===
+
===How can I get the linenumber/column of the currently selected editor?===
<pre>
<pre>
-
                    try {
+
        JTextComponent editor = org.netbeans.api.editor.EditorRegistry.lastFocusedComponent();
-
                        JTextComponent editor = org.netbeans.api.editor.EditorRegistry.lastFocusedComponent();
+
-
                        BaseDocument document = org.netbeans.editor.Utilities.getDocument(editor);
+
-
                        int lineEnd = org.netbeans.editor.Utilities.getLineOffset(document, ed.getSelectionEnd());
+
        //using StyledDocument
-
                        int lineStart = org.netbeans.editor.Utilities.getLineOffset(document, ed.getSelectionStart());
+
        {
-
                        if (lineEnd == lineStart) {
+
            StyledDocument sdocument = (StyledDocument) editor.getDocument();
-
                            //single line
+
 
-
                            System.out.println("currentLine = " + lineStart);
+
            int line = NbDocument.findLineNumber(sdocument, editor.getCaretPosition());
-
                        } else {
+
            int column = NbDocument.findLineColumn(sdocument, editor.getCaretPosition());
-
                            //multiple lines
+
        }
 +
        //using BaseDocument
 +
        {
 +
            try {
 +
                BaseDocument bdocument = Utilities.getDocument(editor);
 +
                int line = Utilities.getLineOffset(bdocument, editor.getCaretPosition());
 +
            } catch (BadLocationException ex) {
 +
                Exceptions.printStackTrace(ex);
 +
            }
 +
        }
-
                        }
 
-
                    } catch (BadLocationException ex) {
 
-
                        Exceptions.printStackTrace(ex);
 
-
                    }
 
</pre>
</pre>
--------
--------
Applies to: NetBeans 4.0 and newer
Applies to: NetBeans 4.0 and newer

Revision as of 13:23, 5 March 2016

How can I get the position of the caret in the currently selected editor window?

You need to first get the selected node (which if the Editor is selected, should correspond to the file being edited); get the most recent editor pane open on it; and then access the caret:

Node[] n = TopComponent.getRegistry().getActivatedNodes();
if (n.length == 1) {
    EditorCookie ec = (EditorCookie) n[0].getCookie(EditorCookie.class);
    if (ec != null) {
        JEditorPane[] panes = ec.getOpenedPanes();
        if (panes.length > 0) {
            int cursor = panes[0].getCaret().getDot();
            String selection = panes[0].getSelectedText();
            // USE selection
        }
    }
}


How can I get the linenumber/column of the currently selected editor?

        JTextComponent editor = org.netbeans.api.editor.EditorRegistry.lastFocusedComponent();

        //using StyledDocument
        {
            StyledDocument sdocument = (StyledDocument) editor.getDocument();

            int line = NbDocument.findLineNumber(sdocument, editor.getCaretPosition());
            int column = NbDocument.findLineColumn(sdocument, editor.getCaretPosition());
        }
        //using BaseDocument
        {
            try {
                BaseDocument bdocument = Utilities.getDocument(editor);
                int line = Utilities.getLineOffset(bdocument, editor.getCaretPosition());
            } catch (BadLocationException ex) {
                Exceptions.printStackTrace(ex);
            }
        }


Applies to: NetBeans 4.0 and newer

Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo