1. Code
  2. Coding Fundamentals
  3. Tools

Quick Introduction: Flash CheckBox and RadioButton Components

Scroll to top
7 min read

In this Quick Tip on the Flash Professional Components, we will be taking a look at the CheckBox and RadioButton Components.


Brief Overview

In the preview you see a single checkbox at the top. When this checkbox is selected the checkbox's label will change to say "Checked" or "Not Checked".

With the bottom six checkboxes you can select what sports you are interested in and the text area below will update to show the changes. With the radio buttons on the right you are only able to select one option; the label will change each time you make a selection and update to say which option you have chosen.


Step 1: Setting up the Document

Open a new Flash Document and set the following properties:

  • Document Size: 550x400px
  • Background Color: #FFFFFF

Step 2: Add Components to the Stage

Open the Components panel (Menu > Window > Components or CTRL+F7).

Drag four labels, seven checkboxes, three radio buttons and one text area to the stage.

In the Properties panel give the first checkbox the instance name "toggleCheckBox".

If the Properties panel is not showing go to Menu > Window > Components or press CTRL+F3.

Set the checkbox's X to 5.00 and its Y to 21.00.

Note: Now follows a fairly repetitive process until we have all our components set up - hang in there :)

In the Properties panel give the second checkbox the instance name "swimmingCheckBox". Set the checkbox's X to 5.00 and its Y to 91.00.

In the Properties panel give the third checkbox the instance name "footBallCheckBox". Set the checkbox's X to 116.00 and its Y to 191.00.

In the Properties panel give the fourth checkbox the instance name "hikingCheckBox". Set the checkbox's X to 5.00 and its Y to 127.00.

In the Properties panel give the fifth checkbox the instance name "soccerBox". Set the checkbox's X to 116.00 and its Y to 127.00.

In the Properties panel give the sixth checkbox the instance name "boxingCheckBox". Set the checkbox's X to 5.00 and its Y to 161.00.

In the Properties panel give the seventh checkbox the instance name "baseballCheckBox". Set the checkbox's X to 116.00 and its Y to 161.00.

In the Properties panel give the first label the instance name "sportsLabel". Set the label's X to 5.00 and its Y to 57.00.

In the Properties panel give the second label the instance name "interestLabel". Set the label's X to 5.00 and its Y to 191.00.

In the Properties panel give the third label the instance name "attendingLabel". Set the label's X to 278.00 and its Y to 21.00.

In the Properties panel give the fourth label the instance name "willAttendLabel". Set the label's X to 278.00 and its Y to 143.00.

In the Properties panel give the first radio button the instance name "yesRadio". Set the radio button's X to 278.00 and its Y to 51.00.

In the Properties panel give the second radio button the instance name "noRadio". Set the radio button's X to 367.00 and its Y to 51.00.

In the Properties panel give the third radio button the instance name "notSureRadio". Set the radio button's X to 278.00 and its Y to 88.00.

In the Properties panel give the text area the instance name "sportsTA". Set the text area's X to 5.00 and its Y to 223.00.


Step 3: Preparing the .as File and Importing the Classes

Create a new ActionScript file and give it the name "Main.as".

We will be declaring our components in this Main.as file so we need to turn off "auto declare stage instances". The benefit of
doing this is that you get code hinting for the instance.

Go to Menu > File > Publish Settings. Click on Settings next to "Script[Actionscript 3]"

Uncheck "automatically declare stage instances".

In Main.as, open the package declaration and Import the classes we will be using by adding the following code:

1
2
package 
3
{
4
5
    //Import our controls

6
    import fl.controls.Label;
7
    import fl.controls.CheckBox;
8
    import fl.controls.TextArea;
9
    import fl.controls.RadioButton;
10
11
12
13
    //Needed to AutoSize the Text in our Labels

14
    import flash.text.TextFieldAutoSize;
15
16
    //Need to put our radio Buttons into a Group

17
    import fl.controls.RadioButtonGroup;
18
	
19
    //Need to extend Movie Clip

20
    import flash.display.MovieClip;
21
22
    //Need for our checkboxes

23
    import flash.events.Event;

Step 4: Set up the Main Class

Add the Class, extend Movie Clip and set up our Constructor Function.

Add the following to Main.as:

1
2
public class Main extends MovieClip
3
{
4
    //The components

5
    public var interestLabel:Label;
6
    public var sportsLabel:Label;
7
    public var attendingLabel:Label;
8
    public var willAttendLabel:Label;
9
    public var toggleCheckBox:CheckBox;
10
    public var labelCheckBox:CheckBox;
11
    public var swimmingCheckBox:CheckBox;
12
    public var hikingCheckBox:CheckBox;
13
    public var boxingCheckBox:CheckBox;
14
    public var footballCheckBox:CheckBox;
15
    public var soccerCheckBox:CheckBox;
16
    public var baseballCheckBox:CheckBox;
17
    public var sportsTA:TextArea;
18
    public var yesRadio:RadioButton;
19
    public var noRadio:RadioButton;
20
    public var notSureRadio:RadioButton;
21
22
    //Need an array for the sports CheckBoxes

23
    private var sportsCheckBoxes:Array;
24
25
    public function Main()
26
    {
27
            setupLabels();
28
            setupCheckBoxes();
29
            setupRadioButtons();
30
    }

Step 5: Functions in the Main Constructor

Here we define the setupLabels(), setUpCheckBoxes(), and setupRadioButtons() functions.

In the setupCheckBoxes() function we put all the sports checkboxes into an array; this way we can loop through them and add a single event listener to each one of them, saving us from having to manually type addEventListener() each time.

For the setupRadioButtons() we have used a radioButtonGroup. Radio buttons are intended for only one of a group to be selected at a time. When we add a radio button to a group we are stating which radio buttons it belongs with.

Add the following to the Main.as:

1
2
private function setupLabels():void
3
{
4
    //Sets the text on the label

5
    sportsLabel.text = "What sports interest you?";
6
    interestLabel.text = "Your Interests Are:";
7
    attendingLabel.text="Will you be attending the event?";
8
    willAttendLabel.text="Will be attending = Yes";
9
10
    //Use autosize so our labels can hold all the text

11
    sportsLabel.autoSize = sportsLabel.autoSize = TextFieldAutoSize.LEFT;
12
    interestLabel.autoSize = interestLabel.autoSize = TextFieldAutoSize.LEFT;
13
    attendingLabel.autoSize = attendingLabel.autoSize = TextFieldAutoSize.LEFT;
14
    willAttendLabel.autoSize = willAttendLabel.autoSize = TextFieldAutoSize.LEFT;
15
16
17
18
}
19
20
private function setupCheckBoxes():void
21
{
22
    //sets the text on the labels

23
    toggleCheckBox.label = "Checked";
24
    swimmingCheckBox.label = "Swimming";
25
    hikingCheckBox.label = "Hiking";
26
    boxingCheckBox.label = "Boxing";
27
    footballCheckBox.label = "Football";
28
    soccerCheckBox.label = "Soccer";
29
    baseballCheckBox.label = "BaseBall";
30
31
32
    toggleCheckBox.width = 250;
33
34
35
    toggleCheckBox.selected = true;
36
37
    toggleCheckBox.addEventListener(Event.CHANGE,toggleChange);
38
    //here we put the checkboxes into an array so we can loop through them

39
    //and easily add the same eventListener to each of them

40
    sportsCheckBoxes = new Array(swimmingCheckBox,hikingCheckBox,boxingCheckBox,footballCheckBox,soccerCheckBox,baseballCheckBox);
41
42
        for (var i:int=0; i<sportsCheckBoxes.length; i++)
43
        {
44
45
                sportsCheckBoxes[i].addEventListener(Event.CHANGE,sportsSelected);
46
        }
47
48
49
}
50
51
52
private function setupRadioButtons():void
53
{
54
			
55
        yesRadio.label="Yes";
56
        noRadio.label="No";
57
        notSureRadio.label="Not Sure";
58
        
59
        yesRadio.selected = true;
60
    
61
        //Radio Buttons are intended have only one selected at a time

62
        //by putting them into a group we say which belong with eachother

63
        var attending:RadioButtonGroup = new RadioButtonGroup("attending");
64
        
65
        //here we assign the radio buttons to the group

66
        yesRadio.group = attending;
67
        noRadio.group = attending;
68
        notSureRadio.group = attending;
69
        
70
        yesRadio.addEventListener(Event.CHANGE,willBeAttending);
71
        noRadio.addEventListener(Event.CHANGE,willBeAttending);
72
        notSureRadio.addEventListener(Event.CHANGE,willBeAttending);
73
74
}

Step 6: Event Listeners

Here we'll code our Event Listeners. Add the following to Main.as

1
2
		private function toggleChange(e:Event):void
3
		{
4
			if (e.target.selected == true)
5
			{
6
					e.target.label = "Checked";
7
			}
8
			else
9
			{
10
					e.target.label = "Not Checked";
11
			}
12
		}
13
14
		private function sportsSelected(e:Event):void
15
		{
16
			//we clear the textarea and add the new options below

17
			sportsTA.text = "";
18
19
			for (var i:int=0; i<sportsCheckBoxes.length; i++)
20
			{
21
		        //if the current checkbox is selected we add it to the textArea

22
				if (sportsCheckBoxes[i].selected == true)
23
				{
24
						sportsTA.text +=  sportsCheckBoxes[i].label + "\n";
25
				}
26
			}
27
28
		}
29
				
30
		private function willBeAttending(e:Event):void
31
		{
32
				
33
			//we use the currently selected radios button label to set Will be attending

34
			//to "Yes","No",or "Not Sure"

35
			willAttendLabel.text ="Will be attending = "+e.target.label;
36
		}
37
		
38
	}//close out the class

39
40
}//close out the package

Conclusion

Using radio buttons and checkboxes is a great way to allow your users to make a selection.

You'll notice in the "Component Parameters" panel of the components that you can check and select certain properties:

This image is for the checkbox component.

The properties are as follows for the checkbox component:

  • enabled: a Boolean value that indicates whether the component can accept user input.
  • label: the text label for the component.
  • labelPlacement: Position of the label in relation to the checkBox.
  • selected: a Boolean value that indicates whether a checkbox is toggled in the on or off position.
  • visible: a Boolean value that indicates whether the component instance is visible.

The properties for the radioButton are as follows:

  • enabled: a Boolean value that indicates whether the component can accept user input.
  • groupName: The group name for a radio button instance or group.
  • label: the text label for the component.
  • labelPlacement: Position of the label in relation to the radioButton..
  • selected: a Boolean value that indicates whether a radioButton is toggled in the on or off position.
  • value: A user-defined value that is associated with a radio button..
  • visible: a Boolean value that indicates whether the component instance is visible.

Thanks for reading and keep your eyes open for upcoming component tutorials!