Tuesday Oct 09, 2012

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.

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
« October 2012 »
SunMonTueWedThuFriSat
 
1
2
3
21
   
       
Today