blob: b078b37f2e0b339ce08de37e6ddfd9cb25016496 [file] [log] [blame]
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//
= DevFaqFindCaretPositionInEditor
:jbake-type: wiki
:jbake-tags: wiki, devfaq, needsreview
:jbake-status: published
:keywords: Apache NetBeans wiki DevFaqFindCaretPositionInEditor
:description: Apache NetBeans wiki DevFaqFindCaretPositionInEditor
:toc: left
:toc-title:
:syntax: true
=== 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:
[source,java]
----
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
}
}
}
----
Or
[source,java]
----
org.netbeans.api.editor.EditorRegistry.lastFocusedComponent().getCaretPosition()
----
=== How can I get the linenumber/column of the currently selected editor?
[source,java]
----
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
=== Apache Migration Information
The content in this page was kindly donated by Oracle Corp. to the
Apache Software Foundation.
This page was exported from link:http://wiki.netbeans.org/DevFaqFindCaretPositionInEditor[http://wiki.netbeans.org/DevFaqFindCaretPositionInEditor] ,
that was last modified by NetBeans user Markiewb
on 2016-03-05T13:25:27Z.
*NOTE:* This document was automatically converted to the AsciiDoc format on 2018-02-07, and needs to be reviewed.