Disappearing HBox and Button controls - removeAllChildren addChild Class issue?

Hi everyone,

I have a class that pops up a Panel with some text:Text and buttons (wrapped in an HBox) in it. This class extends a canvas (which I use as my DisplayObject wrapper). I addChild it to a ‘master’ canvas that I’ve setup called Controller.displayer, and then removeChild it from that canvas when it gets closed. I had to play a lot with removeAllChildren to get things displaying correctly.

It all works wonderfully for the most part.

Except for when you call the popup a second time, and pass it a different set of buttons to render in the HBox. No buttons draw.

It’s almost like a caching problem, because if you call the -same- button set upon a subsequent popup.show(), they draw just fine. It’s only when you try and make it draw -different- buttons that nothing seems to show up. (They’re there, and you can step through them using a trace, they just don’t draw to screen).

There was a similar issue trying to get text to change upon a subsequent call. I got around that by forcing a reset to example.text = ‘’; every time the popup.show was called. Why this works, and an explicitly set example.text doesn’t (or seems to just ‘freeze’ on whatever the first setting of that variable was), is just a little confusing.

I have a feeling that I’m just not resetting something, or not re-initialising something correctly. The problem persists if I remove the HBox and try and draw the buttons directly to the var dialog:Panel.

I’ve attached my stripped down code, and would be stoked if one of you gurus could see what I’m missing here…

Thanks

superc


package visual
{
    import core.Controller;
    
    import flash.events.*;

    import mx.containers.Canvas;
    import mx.containers.HBox;
    import mx.containers.Panel;
    import mx.controls.Button;
    import mx.controls.Text;
    
    public class Popup extends Canvas
    {
        protected static var dialog:Panel     = new Panel();
        protected static var title:Text       = new Text();
        protected static var message:Text     = new Text();
        
        public var ACCEPT:Button   = new Button();
        public var TEST:Button     = new Button();
        public var CANCEL:Text     = new Text();

        protected var controlBar:HBox   = new HBox();
                         
        
        public function Popup()
        {  
        }
        
        
        public function show(msg:String, msgTitle:String=null, buttons:Array=null):void
        {
            this.removeAllChildren();
            
            this.onStartup();
            
            this.buildPopup(msg, msgTitle, buttons);
                    
            Controller.displayer.addChild(this);
        }
        
        
        protected function onStartup():void
        {
            // ** Without resetting these the text stays the same as whatever
            // ** it was set to upon the first call. Don't fully understand why,
            // ** shouldn't a subsequent call explictly overwriting the contents do just that?
            // ** I'm guessing it's something about this reset that's not working for my HBox/Buttons
            message.text  = '';
            title.text    = '';
            
            this.ACCEPT.label   = Controller.lang['ACCEPT'];
            this.ACCEPT.name    = 'ACCEPT';
            this.TEST.label     = Controller.lang['ACCEPT'];
            this.TEST.name      = 'TEST';
            this.CANCEL.text    = Controller.lang['OR_CANCEL'];
            this.CANCEL.name    = 'CANCEL';
                        
            this.ACCEPT.removeEventListener(MouseEvent.CLICK, closePopup);
            this.CANCEL.removeEventListener(MouseEvent.CLICK, closePopup);
            this.TEST.removeEventListener(MouseEvent.CLICK, closePopup);
            
            this.ACCEPT.addEventListener(MouseEvent.CLICK, closePopup);
            this.CANCEL.addEventListener(MouseEvent.CLICK, closePopup);
            this.TEST.addEventListener(MouseEvent.CLICK, closePopup);
            
            dialog.width = 200;// hack size for eg.
            
            this.addChild(dialog);
        }
        
        
        
        protected function buildPopup(msg:String, msgTitle:String, buttons:Array):void
        {
            dialog.removeAllChildren();
            
            if (msgTitle != null)
            {
                title.text  = msgTitle;
                title.width = dialog.width - (2*this.paddingRight);
                dialog.addChild(title);
            }
            
            message.text  = msg;
            dialog.addChild(message);
        
            // ** Ensures I have a proverbial clean slate
            this.controlBar.removeAllChildren(); 
            
            // ** And sure enough, we do:
            trace(this.controlBar.numChildren);  // Returns: 0

            this.controlBar.percentWidth  = 100;
            
            // ** And then this should add the contents
            if (buttons == null) this.controlBar.addChild(this.OK);
            else for each (var btn:* in buttons) this.controlBar.addChild(btn);
            
            // ** Which it apparently does
            trace(this.controlBar.numChildren); // Returns: <however many buttons were called>
            
            // ** Note: Stepping through the children returns the *actual* button objects,
            // ** they're just somehow not drawing to the display
            
            dialog.addChild(this.controlBar);
        }
        
        
        protected function closePopup(e:Event):void
        {
            Controller.displayer.removeChildAt(Controller.displayer.numChildren-1);
        }

    }
}