JEditorPane Code Completion (Part 1)

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.

Update. Part 2 simplifies and corrects this blog entry.

Comments:

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

Posted by Neil C Smith on October 10, 2012 at 07:26 AM PDT #

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

Posted by Geertjan on October 10, 2012 at 08:37 AM PDT #

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!

Posted by Steven Yi on October 11, 2012 at 08:00 AM PDT #

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

https://blogs.oracle.com/geertjan/entry/jeditorpane_code_completion_part_2

Posted by Geertjan on October 12, 2012 at 01:20 PM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
12
13
14
21
22
23
24
25
26
27
28
29
30
   
       
Today