Checkboxes *need* to have a dataProvider?

Servoy 3.5.3

Why is it required that a check box have a dataProvider attached to it so that its assigned onAction method can be called? Is there a way around this?

I am trying to use the checkbox to flip/flop the readOnly parameters of some other fields on the current form, but the simple method I have attached to its onAction that does this never gets called unless there is a dataPrivider attached to the checkbox. I have no desire to save the state of this box in the DB and I feel that going down the path of adding a global to this field will just clutter up my list of globals with stuff that I really don’t care about.

Hi Ryan,

try defining a calc which returns nothing…

;

this calc can be attched to the checkbox and will not be stored in the db.
This technique reduces the need for globals.

Nice little hack :D

That seemed to do the trick, thanks.

Hi Ryan,

Not sure why the method will not run onAction, sorry :(

Whenever I need to represent this sort of on/off state and NOT record it, I tend to use a button which uses and only shows its imageMedia property.

I can then use two images of a checked & an unchecked state and add:

elements.button.setImageURL('media:///??????????');
``` to my method to alter the icon being displayed as appropriate based on your readOnly on or off condition.

Neat and gets away from adding any extra columns or globals

Cheers
Harry

Hi Ryan,

If you really just want to use a checkbox with no dataprovider (of any kind) attached you can use the JCheckBox bean (see beans window).
The trick is just how to add a Servoy method to the bean.
Jeff Bader wrote a nice article on ServoyMagazine about this.

In short you do this:
Add a JCheckBox bean on your form and give it an appropriate name.
Then create a (global) method that is triggered ONLY ONCE like when the solution loads.

var listener = new Packages.java.awt.event.MouseListener( { mouseClicked: globals.yourCheckboxMethod } );
forms.yourFormName.elements.yourJCheckBoxBeanName.addMouseListener(listener);

This will add a listener to your checkbox bean.
However triggering this method more than once will add more listeners to it. So when you click the checkbox ALL those listeners will trigger then, so make sure you add only 1 listener.

Now create a checkbox method you want to fire when the checkbox is changed. Make this is a global method and not a form method to avoid possible memory leaks (see comments in ServoyMagazine article).

And that’s it! :)

Hope this helps.