Creating a Checkbox


/ Published in: ActionScript 3
Save to your folder(s)



Copy this code and paste it in your HTML
  1. package {
  2. import flash.display.*;
  3. import flash.events.*;
  4. import flash.text.*;
  5.  
  6. public class CheckBox extends Sprite {
  7.  
  8. private var _label:FormattedTextField;
  9. private var _icon:CheckBoxIcon;
  10. private var _isChecked:Boolean = false;
  11.  
  12. public function CheckBox( name:String, value:* ) {
  13.  
  14. build( );
  15. setupEventListeners( );
  16. layout( );
  17. }
  18.  
  19.  
  20. private function build():void {
  21.  
  22. _icon = new CheckBoxIcon( );
  23.  
  24. _label = new TextField( );
  25. _label.autoSize = TextFieldAutoSize.LEFT;
  26. _label.text = value.label;
  27. _label.selectable = false;
  28. addChild( _icon );
  29. addChild( _label );
  30. }
  31.  
  32.  
  33. private function layout():void {
  34.  
  35. _label.x = _icon.x + _icon.width + 5;
  36. _label.y = _icon.x + (_icon.height - _label.height) * 0.5;
  37. }
  38.  
  39. private function setupEventListeners():void {
  40.  
  41. addEventListener( MouseEvent.CLICK , clickListener );
  42. }
  43.  
  44. public function set selected( pValue:Boolean ):void {
  45. _isChecked = pValue;
  46. }
  47.  
  48. public function get selected():Boolean {
  49. return _isChecked;
  50. }
  51.  
  52. private function clickListener(e:MouseEvent):void {
  53. if(selected)
  54. {
  55. _icon.uncheck( );
  56. selected = false;
  57. }
  58. else
  59. {
  60. _icon.check( );
  61. selected = true;
  62. }
  63. }
  64. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.