Geertjan's Blog

  • October 9, 2012

JEditorPane Code Completion (Part 1)

Geertjan Wielenga
Product Manager

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");
FileObject fob;
try {
    fob = FileUtil.getConfigRoot().createData("tmp.java");
    DataObject dob = DataObject.find(fob);
    DialogBinding.bindComponentToFile(fob, 0, 0, jEditorPane1);
} catch (IOException 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.

Update. Part 2 simplifies and corrects this blog entry.

Join the discussion

Comments ( 4 )
  • Neil C Smith Wednesday, October 10, 2012

    Are you aware of the AutoComplete library from the guy behind RText - http://fifesoft.com/projects.php It's potentially a better lightweight solution for this sort of use, and also works with any Swing text component. The NB editor is great (possibly second to none! :-) ), but seems overkill for this kind of usage.

    Best wishes, Neil

  • Geertjan Wednesday, October 10, 2012

    Wow, didn't know about that page, looks very useful, thanks!

  • Steven Yi Thursday, October 11, 2012

    I was looking at trying to get the NB Code Completion working without rest of the Editor modules a few weeks ago, but had to set it aside. I had also looked at the code completion for RText. It's still marked as in development, and the API for it did not seem to pull information completely dynamically, as the NB Code Completion does. (Also, I did not see a way to have the secondary information pane be visible only if necessary, and it seemed to be up all the time.) I've been planning to dig into the NB Code Completion sources to see if relevant pieces could be called directly; hopefully will look into this the next few days. If anyone else knows a way, I'd love to know too!

  • Geertjan Friday, October 12, 2012

    Fixed it, use the Plain Editor instead of Java Editor, which means two JARs instead of dozens:


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.