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:
    okButton.setEnabled(false);
    
    ageField.setName("Age field");
    
    final ValidationGroup group = validationPanel1.getValidationGroup();
    
    group.add(ageField, Validators.REQUIRE_NON_EMPTY_STRING,
            Validators.NO_WHITESPACE,
            Validators.REQUIRE_VALID_INTEGER);
    
    ageField.getDocument().addDocumentListener(new DocumentListener() {
        @Override
        public void insertUpdate(DocumentEvent e) {
            checkValidation();
        }
        @Override
        public void removeUpdate(DocumentEvent e) {
            checkValidation();
        }
        @Override
        public void changedUpdate(DocumentEvent e) {
            checkValidation();
        }
        private void checkValidation() {
            Problem validateAll = group.validateAll();
            if (validateAll.isFatal()) {
                okButton.setEnabled(false);
            } else {
                okButton.setEnabled(true);
            }
        }
    });

That's all.

Read all the related info here: http://kenai.com/projects/simplevalidation/pages/Home

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.

Comments:

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:

@Override
public void clearProblem() {
ok.setEnabled(true);
}

@Override
public void setProblem(Problem problem) {
if (problem == null) {
ok.setEnabled(true);
} else {
ok.setEnabled(!problem.isFatal());
}
}

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 http://kenai.com/projects/simplevalidation/forums/forum/topics/4671-1-0-Release-

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

it is showing me the error

run:
Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: org/openide/util/NbBundle
at org.netbeans.validation.api.builtin.EmptyStringIllegalValidator.validate(EmptyStringIllegalValidator.java:54)
at org.netbeans.validation.api.builtin.EmptyStringIllegalValidator.validate(EmptyStringIllegalValidator.java:50)
at org.netbeans.validation.api.builtin.Validators.validate(Validators.java:488)
at org.netbeans.validation.api.builtin.Validators.validate(Validators.java:63)
at org.netbeans.validation.api.builtin.AndValidator.validate(AndValidator.java:61)
at org.netbeans.validation.api.conversion.StringToDocumentConverter$DocValidator.validate(StringToDocumentConverter.java:69)
at org.netbeans.validation.api.conversion.StringToDocumentConverter$DocValidator.validate(StringToDocumentConverter.java:59)
at org.netbeans.validation.api.ui.ValidationGroupImpl$3V.validate(ValidationGroupImpl.java:478)
at org.netbeans.validation.api.ui.ValidationGroupImpl.validateAll(ValidationGroupImpl.java:524)
at org.netbeans.validation.api.ui.ValidationGroup.validateAll(ValidationGroup.java:161)
at org.netbeans.validation.api.ui.ValidationPanel.addNotify(ValidationPanel.java:218)
at java.awt.Container.addNotify(Container.java:2592)
at javax.swing.JComponent.addNotify(JComponent.java:4665)
at java.awt.Container.addImpl(Container.java:1062)
at javax.swing.JLayeredPane.addImpl(JLayeredPane.java:209)
at java.awt.Container.add(Container.java:903)
at javax.swing.JRootPane.setContentPane(JRootPane.java:607)
at javax.swing.JFrame.setContentPane(JFrame.java:676)
at login.<init>(login.java:11)
at login$1.run(login.java:43)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)

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 #

Hi,
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?

Thanks,
Ranga.

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 SimpleDefaultDecorator.java. 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.

http://kenai.com/projects/simplevalidation/forums/forum/topics/494263-Simple-Validation-API-small-Icon-Position-Lossed

Anyhow, thanks for the great blog.

Stephen

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

Post a Comment:
  • HTML Syntax: NOT allowed
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
« April 2015
SunMonTueWedThuFriSat
   
4
5
11
12
26
27
28
29
30
  
       
Today