JEditorPane Code Completion
- by Geertjan
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.