Validating a Form in the NetBeans Platform

Validation of a form in a NetBeans Platform application. First some pics:

How to do it?


  1. Wrap the "ide/modules/ext/ValidationAPI.jar", i.e., from your NetBeans IDE installation, into your application.


  2. Extend the palette by right-clicking it, choosing Palette Manager, then "Add from JAR", then browsing to the above API and selecting the "ValidationPanel". Drag it from the palette and drop it on your TopComponent, where it will be named "validationPanel1" by default.


  3. Then add this code to the constructor:
    ageField.setName("Age field");
    final ValidationGroup group = validationPanel1.getValidationGroup();
    group.add(ageField, Validators.REQUIRE_NON_EMPTY_STRING,
    ageField.getDocument().addDocumentListener(new DocumentListener() {
        public void insertUpdate(DocumentEvent e) {
        public void removeUpdate(DocumentEvent e) {
        public void changedUpdate(DocumentEvent e) {
        private void checkValidation() {
            Problem validateAll = group.validateAll();
            if (validateAll.isFatal()) {
            } else {

That's all.

Read all the related info here:

I really wish this were a standard part of the NetBeans Platform (in the "platform" cluster) rather than an external library. Still, better than nothing that it is in the NetBeans IDE's installation directory.


You should get away without the listener altogether by introducing a ValidationUI that handles the button (attached to the panel's validation group). That way you will also avoid duplicate validation that is probably happening now.

BTW we have a swt bridge for the library internally at our company and it works just fine.

Posted by Milos Kleint on January 10, 2011 at 02:08 PM PST #

You can get rid of the DocumentListener part by making the form implement ValidationUI. You can then control the enabled state of your OK button from within your implementations of setProblem and clearProblem:

public void clearProblem() {

public void setProblem(Problem problem) {
if (problem == null) {
} else {

Posted by Danie Palm on January 10, 2011 at 02:48 PM PST #

btw: the downloadable binary at the simple validation site is fairly old, I would suggest everyone to build the latest from sources.

more at

Posted by Milos Kleint on January 10, 2011 at 06:02 PM PST #

it is showing me the error

Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: org/openide/util/NbBundle
at org.netbeans.validation.api.builtin.EmptyStringIllegalValidator.validate(
at org.netbeans.validation.api.builtin.EmptyStringIllegalValidator.validate(
at org.netbeans.validation.api.builtin.Validators.validate(
at org.netbeans.validation.api.builtin.Validators.validate(
at org.netbeans.validation.api.builtin.AndValidator.validate(
at org.netbeans.validation.api.conversion.StringToDocumentConverter$DocValidator.validate(
at org.netbeans.validation.api.conversion.StringToDocumentConverter$DocValidator.validate(
at org.netbeans.validation.api.ui.ValidationGroupImpl$3V.validate(
at org.netbeans.validation.api.ui.ValidationGroupImpl.validateAll(
at org.netbeans.validation.api.ui.ValidationGroup.validateAll(
at org.netbeans.validation.api.ui.ValidationPanel.addNotify(
at java.awt.Container.addNotify(
at javax.swing.JComponent.addNotify(
at java.awt.Container.addImpl(
at javax.swing.JLayeredPane.addImpl(
at java.awt.Container.add(
at javax.swing.JRootPane.setContentPane(
at javax.swing.JFrame.setContentPane(
at login.<init>(
at login$
at java.awt.event.InvocationEvent.dispatch(
at java.awt.EventQueue.dispatchEvent(
at java.awt.EventDispatchThread.pumpOneEventForFilters(
at java.awt.EventDispatchThread.pumpEventsForFilter(
at java.awt.EventDispatchThread.pumpEventsForHierarchy(
at java.awt.EventDispatchThread.pumpEvents(
at java.awt.EventDispatchThread.pumpEvents(

Posted by guest on October 17, 2011 at 07:04 AM PDT #

You can see the problem in your code, the stack trace is very clear (and next time you post a whole stack trace in my blog, I will not publish it and I will block you from posting anything else):

java.lang.NoClassDefFoundError: org/openide/util/NbBundle

So, you need to add a dependency on the Utilities API, which provides the NbBundle class.

Posted by Geertjan on October 17, 2011 at 07:12 AM PDT #

This is a great post and it worked fine for me in a platform app. in Editor mode. However, when I did the same to a sliding component, the 'red bulb' that attaches to the top of the text field is not appearing - this happends when the tc is not 'pinned' - however, when I move the slider away, I see the 'red bulb' still present - I am thinking the bulb is shown in a layered pane and so, doesn't show up in the sliding mode. It works fine when I pin the tc.
Is there a workaround for this issue?


Posted by Ranga on December 25, 2011 at 10:41 PM PST #

I know this topic is old, but I fixed a scrolling issue in the simple validation code where the decorator icons did not move with their decorated component. The changes are about two dozen lines of code, all in I have a working jar for my project, but would like to get the fixes into the jar included with Netbeans. How do I do that?

I posted the details on the project's forum, but it seems to be a dead project.

Anyhow, thanks for the great blog.


Posted by guest on February 21, 2012 at 12:29 PM PST #

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.


« July 2016