Tutorial for Editor Code Completion API

When you press Ctrl-Space in the IDE, a code completion box pops up with words that you can use to complete the word under the curser. Here's a new tutorial that explains how you can create code completion functionality for your own MIME types or for one of the existing ones:

NetBeans Code Completion Tutorial

The tutorial replaces an older one. It's much better because it focuses specifically on code completion, not on extraneous stuff like web service clients and Options window integrations. In the end, you'll have a simple code completion box in HTML files, listing countries that can be selected and inserted into the editor:

The tutorial applies to NetBeans IDE 6.0 and upwards. It also contains a link to a plugin that provides the sources of the sample, in case you get stuck.


Very interesting. We have a desire in our team to get code completion for JPA queries. However, JPA is an embedded DSL inside string annotations. Does the approach described in the tutorial also apply if one wanted to "sub-tokenize" and provide completion inside a String token of a x-java MIME type?

Posted by Casper Bang on August 08, 2008 at 03:59 AM PDT #

In the tutorial, there should be "at " in:
public CompletionTask createTask(int i,at JTextComponent jTextComponent) {

Posted by Vincent Cantin on August 09, 2008 at 12:33 AM PDT #

After trying the cool tutorial, a few observations newbies like myself might choke on:

In "Implementing the Completion Item Class" item 5 you instruct the user to return the text member instance from the getInsertPrefix, and in the line under, you say to return null from
getInsertPrefix. The former appears to be correct.

Also, you should probably have the user add the "Utilities API" module library such as to later be able to resolve Uilities.loadImage(...).

In "Implementing the Action" item 1, you forgot to wrap doc.insertString with a try...catch (though visible later in "Implementing the Filter" item 3).

The download includes the compiled and packaged .nbm, not the actual source code.

Posted by Casper Bang on August 10, 2008 at 04:40 AM PDT #

"The download includes the compiled and packaged .nbm, not the actual source code."

Rubbish. Install it and you have a sample in the New Project wizard, as shown on the plugin's page. Go back there and look at the screenshots.

Thanks for the other comments, I'll look into them.

Posted by Geertjan on August 10, 2008 at 04:46 AM PDT #

Ohh, the .nbm is a sample extension to NetBeans. In that case, all is well and fine. :)

Posted by Casper Bang on August 10, 2008 at 05:37 AM PDT #

Exactly! Have fun with it (and read the next blog entry too if you haven't yet).

Posted by Geertjan on August 10, 2008 at 05:43 AM PDT #

Thanks for the tutorial information. It is very easy to follow.

Following the example, I've begun adding code completion to my plug-in for the NWScript scripting language. I've managed to extend the example to display one piece of text (full function declaration or constant with datatype) and insert the useable code (the constant name, or function name with datatypes or default values for parameters).

My question is - how does Netbeans display the datatype for Java functions on the right side of the completion window all nice and neat? Is there a class I can look at as an example?

Posted by Karl Nickels on August 12, 2008 at 02:16 AM PDT #

I continued on without worrying about the "neatness" of my completion window and ran into another issue.

The implementation I chose for storing my lists is apparently an unwise choice. Needless to say, I made a static class with two static string arrays, which worked fine for the initial testing with a few entries, but Java apparently does not like a string array with over 8,000 entries, much less two.

Should I be reading the entries from a data file instead? Should I choose something more dynamic like ArrayList? Or, if you could point me to the source of the relevent section of the Netbeans Java editor, I can try to follow that. I have been unsuccessful in locating it so far.

Posted by Karl Nickels on August 12, 2008 at 07:06 AM PDT #

great codes

Posted by msn avatarları on August 26, 2008 at 09:41 AM PDT #

The link to the tutorial is broken, use this one instead: http://platform.netbeans.org/tutorials/nbm-code-completion.html

Posted by Johannes on December 17, 2008 at 12:20 AM PST #

I am newbie using of Java. I tried getInsertPrefix too. But it doesnt work for me...

Posted by Dedicated on February 02, 2009 at 07:29 AM PST #

we say this kind of infos as kolbasti

Posted by kolbastı on February 04, 2009 at 02:01 AM PST #

your labor health

Posted by Tabela on February 22, 2009 at 08:59 PM PST #


Posted by Jobs on April 07, 2009 at 01:06 PM PDT #

Thanks for your article..

Posted by Web Hosting on May 06, 2009 at 12:31 PM PDT #

thank you blogs.sun!

Posted by joomla hosting on May 12, 2009 at 06:20 PM PDT #

Thanks for your article..

Posted by sunucu on May 12, 2009 at 06:22 PM PDT #

thank you bro!...

Posted by beylikdüzü halı yıkama on July 02, 2009 at 11:21 PM PDT #

hello.. thank you for this article.

Posted by porno on July 14, 2009 at 03:26 PM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed

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.


« April 2014