DevFaqFindCaretPositionInEditor

(Difference between revisions)
Line 1: Line 1:
-
__NOTOC__
 
__NOTOC__
__NOTOC__
===How can I get the position of the caret in the currently selected editor window?===
===How can I get the position of the caret in the currently selected editor window?===

Revision as of 15:44, 6 November 2009

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
        }
    }
}

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