JEditorPane Code Completion

Posted by Geertjan on Oracle Blogs See other posts from Oracle Blogs or by Geertjan
Published on Tue, 9 Oct 2012 21:57:33 +0000 Indexed on 2012/10/10 3:46 UTC
Read the original article Hit count: 210

Filed under:

Code completion in a JEditorPane:

Unfortunately, a lot of this solution depends on the Java Editor support in the IDE. Therefore, to use it, in its current state, you'll need lots of Java Editor related JARs even though your own application probably doesn't include a Java Editor.

A key thing one needs to do is implement the NetBeans Code Completion API, using the related tutorial in the NetBeans Platform Learning Trail, but register the CompletionProvider as follows:

@MimeRegistration(mimeType = "text/x-dialog-binding", service = CompletionProvider.class)

Then in the TopComponent, include this code, which will bind all the completion providers in the above location, i.e., text/x-dialog-binding, to the JEditorPane:

EditorKit kit = CloneableEditorSupport.getEditorKit("text/x-java");
jEditorPane1.setEditorKit(kit);

FileObject fob;
try {
    fob = FileUtil.getConfigRoot().createData("tmp.java");
    DataObject dob = DataObject.find(fob);
    jEditorPane1.getDocument().putProperty(
            Document.StreamDescriptionProperty,
            dob);
    DialogBinding.bindComponentToFile(fob, 0, 0, jEditorPane1);
    jEditorPane1.setText("Egypt");
} catch (IOException ex) {
    Exceptions.printStackTrace(ex);
}

Not a perfect solution, a bit hacky, with a high overheard, but a start nonetheless. Someone should look in the NetBeans sources to see how this actually works and then create a generic solution that is not tied to the Java Editor.

© Oracle Blogs or respective owner

Related posts about /NetBeans IDE