X

Geertjan's Blog

  • January 10, 2011

Validating a Form in the NetBeans Platform

Geertjan Wielenga
Product Manager
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.

Join the discussion

Comments ( 7 )
  • Milos Kleint Monday, January 10, 2011

    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.


  • Danie Palm Monday, January 10, 2011

    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());

    }

    }


  • Milos Kleint Tuesday, January 11, 2011

    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-


  • guest Monday, October 17, 2011

    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)


  • Geertjan Monday, October 17, 2011

    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.


  • Ranga Monday, December 26, 2011

    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.


  • guest Tuesday, February 21, 2012

    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


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.