Return to Snippet

Revision: 13950
at May 15, 2009 10:01 by sidneydekoning


Updated Code
package {
  import flash.display.*;
  import flash.events.*;
  import flash.text.*;

  public class CheckBox extends Sprite {

	private var _label:FormattedTextField;
	private var _icon:CheckBoxIcon;
	private var _isChecked:Boolean = false;
		
	public function CheckBox( name:String, value:* ) {
		
		build( );
		setupEventListeners( );
		layout( );
	}

		
	private function build():void {
		
		_icon = new CheckBoxIcon( );
		
		_label = new TextField(  );
		_label.autoSize = TextFieldAutoSize.LEFT;
		_label.text = value.label;
		_label.selectable = false;
		addChild( _icon );
		addChild( _label );
	}

		
	private function layout():void {
			
		_label.x = _icon.x + _icon.width + 5;
		_label.y = _icon.x + (_icon.height - _label.height) * 0.5;
	}

	private function setupEventListeners():void {
	
		addEventListener( MouseEvent.CLICK , clickListener );
	}
		
	public function set selected( pValue:Boolean ):void {
		_isChecked = pValue;
	}
		
	public function get selected():Boolean {
		return _isChecked;
	}

	private function clickListener(e:MouseEvent):void {
		if(selected) 
		{
			_icon.uncheck( );
			selected = false;
		} 
		else 
		{
			_icon.check( );
			selected = true;
		}
	}
}

Revision: 13949
at May 14, 2009 09:22 by sidneydekoning


Initial Code
package {
  import flash.display.*;
  import flash.events.*;
  import flash.text.*;

  public class CheckBox extends Sprite {
    private var label:TextField;    // The checkbox's text label
    private var icon:CheckBoxIcon;  // The checkbox's graphical icon
    private var checked:Boolean;    // Flag indicating whether the
                                    // checkbox is currently checked
    public function CheckBox (msg:String) {
      checked = false;

      icon = new CheckBoxIcon(  );

      label = new TextField(  );
      label.text = msg;
      label.autoSize = TextFieldAutoSize.LEFT;
      label.selectable = false;

      label.x = icon.x + icon.width + 5;

      addChild(icon);
      addChild(label);

      addEventListener(MouseEvent.CLICK, clickListener);
    }

    private function clickListener (e:MouseEvent):void {
        if (checked) {
            icon.uncheck(  );
            checked = false;
        } else {
            icon.check(  );
            checked = true;
        }
    }
  }
}

Initial URL


Initial Description


Initial Title
Creating a Checkbox

Initial Tags


Initial Language
ActionScript 3