Tuesday, August 22, 2006

NetBeans Platform: Options Dialog isValid() or implement thy TODOs

The Options Dialog API allows an invalid state for the panels.This is normal, it could happen that user changes lead to an invalid state and you don't want to set some magic default. So, you force the user to make all the changes until the panel is in a valid state.

For example: I have an user that may be a minor. If he's a minor, the parent's name must be set. I have 3 situations:

  • he's an adult. Panel state is valid. Ok and cancel button are active, no warning. All is good.
  • he's an minor with an empty parent name. You get the red text warning (which is just Swing from my JPanel, no Platform stuff here) and the OK button is disabled. Noticed that ? This is where the isValid() does its work.
  • he is a minor with a non-empty parent name. The OK button is enabled (isValid contribution) and no more red warnings (pure Swing code): Fun stuff no ?

The way this is doable is: just look at the TODOs.

  1. Implement the load() and store() methods in the generated Panel to have persistence.
  2. Implement the valid() method in the Panel. In my case is something as simple as
     return adult || (!adult && parentName.getText().trim().length()>0);

  3. Then implement the final TODO from the constructor: listen to changes in form fields and call controller.changed() In my case I just call controller.changed() when the radio buttons are pressed and also in a DocumentListener for the text (to detect if the parent name changes and provide instant feedback).

And that was all it was needed. Now I have a simple, interractive with possible invalid states option panel.

If this was helpful for you then maybe you'll like reading the other NetBeans Platform-related posts.

Emilian Bold
Java-loving consulting services from Timisoara, Romania.

1 comment:

Burattino said...

Why you didn't link the TODOs?
I't could be useful...

The Trouble with Harry time loop

I saw The Trouble with Harry (1955) a while back and it didn't have a big impression on me. But recently I rewatched it and was amazed a...