7 Replies Latest reply on Feb 14, 2007 11:56 PM by doug777

    Adding icon to Button

    doug777 Level 1
      How do I put an icon instead of a label into a button?

      Say I have a project myProject with two folders icons and asfiles. In icons I have a file called myIcon.gif and in asfiles a file called myASFile.

      In myASFile I define a button var myButton:Button = new Button();

      Then how do I get myIcon in the button?

      myButton.setStyle("icon", ????);

      According to the docs ???? should specify a class, but I don't understand how myIcon.gif gets to become a class.

        • 1. Re: Adding icon to Button
          doug777 Level 1
          Found some more info on how to do this, but still having problems.

          In the initialization path:

          public var iconA:Class;

          But when I call on the asset:

          myButton.setStyle("icon", "{iconA}");

          I get the error:

          Type Coercion failed: cannot convert "{iconA}" to Class.

          Anyone know what the solution is?

          • 2. Re: Adding icon to Button
            doug777 Level 1
            The correct way to do this is as follows:

            import mx.core.BitmapAsset;

            private var iconA:Class;
            private var icA:BitmapAsset = BitmapAsset(new iconA());

            var upBtn:Button = new Button();

            • 3. Re: Adding icon to Button
              jpwrunyan Level 1
              Although I certainly think that is a clever work-around, I take issue with the phrase "correct way".

              I think what you wanted to do was this:

              private static var IconClass:Class;


              this.setStyle("icon", IconClass);

              I think that should work without you using addChild().
              • 4. Re: Adding icon to Button
                doug777 Level 1
                Ah so that's how it's done.

                It's a pity that the documentation for ActionScript 3 is so minimal. For users with no Java background like me, fighting through this stuff is a nightmare.

                Thanks very much for your help.

                • 5. Re: Adding icon to Button
                  jpwrunyan Level 1
                  Well, thank me after it works. I'm not 100% certain it will. It works in my case but my case is a little different from yours. If it doesn't work, I still want to know, though. (in my case, I do not use setStyle(), instead I am using it in combination with the Alert.show() method... which I then assume calls setStyle())
                  • 6. Re: Adding icon to Button
                    doug777 Level 1
                    Don't worry, it definitely works.

                    I think you can be confident that this is the solution for most people trying to do this.

                    I have taken the liberty of reinstating the tick mark against your solution as the answer.

                    • 7. Re: Adding icon to Button
                      doug777 Level 1

                      Not only does it work, but the button is automatically resized to fit the size of the picture. With my solution, I had to resize the button manually.

                      And it looks to me as though you have to do manual resizing for the solution offered for mx users in the Flex documentation as well.