Apache NetBeans Wiki Index

Note: These pages are being reviewed.

How can I get a list of open editor windows?

To obtain a reference to the currently selected editor

Node[] arr = TopComponent.getRegistry().getCurrentNodes();
for (int i = 0; i < arr.length; i++) {
    EditorCookie ec = (EditorCookie) arr[i].getCookie(EditorCookie.class);
    if (ec != null) {
        JEditorPane[] panes = ec.getOpenedPanes();
        if (panes != null) {
            // USE panes
        }
    }
}

Note that EditorCookie.getOpenPanes() is deprecated (https://netbeans.org/bugzilla/show_bug.cgi?id=223383). Use org.openide.text.NbDocument.findRecentEditorPane(EditorCookie) instead.

To obtain references to all opened editors

Set<TopComponent> comps = TopComponent.getRegistry().getOpened();
for (TopComponent tc: comps) {
    Node[] arr = tc.getActivatedNodes();
    for (int j = 0; j < arr.length; j++) {
        EditorCookie ec = (EditorCookie) arr[j].getCookie(EditorCookie.class);
        if (ec != null) {
            JEditorPane[] panes = ec.getOpenedPanes();
            if (panes != null) {
                // USE panes
            }
        }
    }
}

Variants to get the Editor TopComponents

Variant A)
    private Collection<TopComponent> getCurrentOpenedEditors() {
        final ArrayList<TopComponent> result = new ArrayList<TopComponent>();
        final WindowManager wm = WindowManager.getDefault();
        for (Mode mode : wm.getModes()) {
            if (wm.isEditorMode(mode)) {
                //result.addAll(Arrays.asList(mode.getTopComponents())); OR even faster
                result.addAll(Arrays.asList(wm.getOpenedTopComponents(mode)));
            }
        }
        return result;
    }
Variant B)
    private TopComponent getCurrentEditor() {
	Set<? extends Mode> modes = WindowManager.getDefault().getModes();
	for (Mode mode : modes) {
	    if ("editor".equals(mode.getName())) {
		return mode.getSelectedTopComponent();
	    }
	}
	return null;
    }

    //or
    private TopComponent getCurrentEditor() {
        Mode editor = WindowManager.getDefault().findMode("editor");
        return editor.getSelectedTopComponent();
    }

<hr/> Reference- Editor Windows Reactivated <hr/>

Apache Migration Information

The content in this page was kindly donated by Oracle Corp. to the Apache Software Foundation.

This page was exported from http://wiki.netbeans.org/DevFaqGetOpenEditorWindows , that was last modified by NetBeans user Markiewb on 2016-10-26T20:35:35Z.

NOTE: This document was automatically converted to the AsciiDoc format on 2018-02-07, and needs to be reviewed.