Creating a check box

How to

Create a check box in your application.

Solution

Construct a check box variable in the root of your class

private var myCheckBox:CheckBox = new CheckBox(); 	
// Placing this at the class root allows other 
// application elements to reference it.

Initialize the attributes for your check box

// Location and size
myCheckBox.setPosition(200,300);
myCheckBox.width = 150;

// Set up checkbox label
myCheckBox.label = "Enable setting";
myCheckBox.labelPlacement = LabelPlacement.TOP; // This is optional, requires LabelPlacement import.


// Call myCheckBoxEvent function when button is clicked.
myCheckBox.addEventListener(MouseEvent.CLICK, myCheckBoxEvent);

// Add check box to stage.
this.addChild(myCheckBox);

Create the function that is called by your check box's click event

private function myCheckBoxEvent(event:MouseEvent) {
	// Output click event to console. myCheckBox.selected 
	// returns true when button is in the down state.
	trace ("myCheckBox has been clicked, current toggle state is:" + myCheckBox.selected);
}

Build requirements

You must include the following classes in your project:

import qnx.fuse.ui.buttons.CheckBox;
import flash.events.MouseEvent;

Discussion

When you create a check box, you declare the CheckBox variable within the class scope and then set its attributes. By default, the check box label appears to the RIGHT of the control. However, including the optional qnx.fuse.ui.buttons.LabelPlacement class within your project allows you to specify which side of your check box the label appears. To add functionality, add a MouseEvent.CLICK listener function to perform any actions that are defined in the function when the check box is clicked. You can also reference the state of the check box anywhere in the class by using the selected property.

comments powered by Disqus