How to add code completion to any JEditorPane

Apache NetBeans Wiki Index

Note: These pages are being reviewed.

You can add the built-in Java code completion from the Netbeans 6 Java editor to any arbitrary JEditorPane. See the code below for how this can be achieved. Note that Netbeans Java editor functionality is derived from an underlying FileObject, so we are essentially creating a dummy Java FileObject and tying our JEditorPane document in with the appropriate hooks.

The end result of this code is that we create a Java context for our JEditorPane. This context initializes code completion with a default class path, and that grants us access to the standard Java APIs (i.e. the code completion box can include classes such as java.lang.String, java.util.List, etc.). However, this context has no visibility into any additional jars nor Java projects. In order to expand this default Java context, you will need to create your own class path provider (see the "Java Support APIs" module).

First, let’s take a look at some of the classes we’ll be using:

  • import java.io.File;

  • import javax.swing.JEditorPane;

  • import javax.swing.text.Document;

  • import javax.swing.text.EditorKit;

  • import org.netbeans.spi.java.classpath.ClassPathProvider; <span style='color:orange;'> from the "Java Support APIs" module</span>

  • import org.openide.filesystems.FileObject; <span style='color:orange;'> from the "File System API" module</span>

  • import org.openide.filesystems.FileUtil; <span style='color:orange;'> from the "File System API" module</span>

  • import org.openide.loaders.DataObject; <span style='color:orange;'> from the "Datasystems API" module</span>

  • import org.openide.text.CloneableEditorSupport; <span style='color:orange;'> from the "Text API" module</span>

  • import org.openide.util.Lookup; <span style='color:orange;'> from the "Utilities API" module</span>

  • import org.netbeans.api.java.source.ui.DialogBinding; <span style='color:orange;'> from the "Java Source UI" module</span>

Now we’re ready to take a look at the actual code:

        JEditorPane editorPane = new JEditorPane();

        // This will find the Java editor kit and associate it with
        // our editor pane. But that does not give us code completion
        // just yet because we have no Java context (i.e. no class path, etc.).
        // However, this does give us syntax coloring.
        EditorKit kit = CloneableEditorSupport.getEditorKit("text/x-java");
        editorPane.setEditorKit(kit);

        // You can specify any ".java" file.
        // If the file does not exist, it will be created.
        // The contents of the file does not matter.
        // The extension must be ".java", however.
        String newSourcePath = "tmp.java";

        File tmpFile = new File(newSourcePath);
        FileObject fob = FileUtil.createData(tmpFile);

        DataObject dob = DataObject.find(fob);
        editorPane.getDocument().putProperty(
                Document.StreamDescriptionProperty,
                dob);

        // This sets up a default class path for us so that
        // we can find all the JDK classes via code completion.
        DialogBinding.bindComponentToFile(fob, 0, 0, editorPane);

        // Last but not least, we need to fill the editor pane with
        // some initial dummy code - as it seems somehow required to
        // kick-start code completion.
        // A simple dummy package declaration will do.
        editorPane.setText("package dummy;");

Applies to: Netbeans 6.0, 6.1 and 6.5. Since 6.7 DialogBinding class was moved to org.netbeans.api.editor package in Editor Library 2 module.

Platforms: All