Geertjan's Blog

  • December 2, 2006

Creating Error Annotations in NetBeans IDE (Part 1)

Geertjan Wielenga
Product Manager
Sometime ago I blogged about a module I was working on for integrating Jazzy Open Source Spell Checker into NetBeans. At the time, the spelling errors were shown in a window on the right of the IDE (click here to see it). That's not an ideal way of dealing with spelling errors. It would be better to mark them in the editor than to display them separately. So, since the last few days I've been learning about error annotations (org.openide.text.Annotation). As a result, my spelling checker is slightly more useful now:

The most important thing I've learnt is that the only thing that is tricky is working out the org.openide.text.Line where the error occurs. Once you know the line, it is all really easy (really, no kidding). Fortunately you can use NbEditorUtilities.getLine(Document doc, int offset, boolean original). (Tip: Check out the other NbEditorUtilities. You never know when you might need them!)

Providing error annotations is a three step process—define the annotation in an XML file (using AnnotationType DTD), register the XML file in the XML layer file, and then refer to the XML file in the code. In the code, you call Annotation.attach(Annotatable anno) on a Line or Line.Part, which is why you need to know the Line object. (Or, you could create your own object that extends Annotatable). To detach the annotation, use Annotation.detach().

So, after creating my XML file, and registering it in the XML layer, I integrated the annotation into my spellchecker module via the ArrayList of misspelled words that Jazzy provides me:

Iterator it = misspelled.iterator();
if (!(it.hasNext())){
while (it.hasNext()) {
next = (String) it.next();
Line myLine = NbEditorUtilities.getLine(doc, texTok.getCurrentWordPosition(), false);

(By the way, the texTok.getCurrentWordPosition() comes from the Jazzy API...)

And here's the definition of the SpellErrorAnnotation:

private static final class SpellErrorAnnotation extends Annotation {
static final SpellErrorAnnotation DEFAULT = new SpellErrorAnnotation();
public String getAnnotationType() {
return "org-netbeans-modules-htmlspellchecker";
public String getShortDescription() {
return "Spelling error: " + next;

I will blog more about error annotations in the coming days. For example, I'll talk about the AnnotationType DTD in some detail.

Today I met Andreas (who I interviewed recently here). He was interested to hear I'd be blogging about error annotations, because he needs them for his Tapestry module. So, I hope my annotations on error annotations will prove to be useful (they'll also find their way into the book we're working on). And where did I meet Andreas? Here in Athens, at a meeting of the Hellenic Java User Group. I didn't feel very positive about my presentation, but I think the ease of rich-client application development on the NetBeans Platform came through pretty clearly. I went through the whole development cycle and we ended up with a simple music application. Roumen's presentation was very solid, I thought, as always. In 40 minutes he created various clients (Visual Web pack and mobility, with an overview of a client in Matisse) for a web service, showing the versatility of the NetBeans IDE. In terms of the range of out of the box technology support in NetBeans IDE (from Java EE to Java ME to Java SE), there really is nothing to compare NetBeans to. Hurray for NetBeans. The JUG event was attended by about 70 people, all showing a lot of interest in NetBeans. Hurray for the Hellenic Java User Group! (Check out the pictures here!)

Join the discussion

Comments ( 3 )
  • paris apostolopoulos Saturday, December 2, 2006
    Thank you Geertjan for your presentation. It was very very clear I really can not understand why you think it was not so good! Maybe the souvlaki from yesterday??huhuhu! Ok hold on because tonight is going to get worse......
  • Geertjan Sunday, December 3, 2006
    Hi Paris! Thanks for the comment and you were right -- things did get a lot worse (but better at the same time)!
  • dlixismmac Tuesday, December 26, 2006
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.