tags:

views:

1172

answers:

4

We are creating a LinkButton programmatically and would like to set it's icon to an image retrieved from the remote server rather than something embedded within the SWF. The .icon property expects a Class but I can't figure out how to create one equivalent to an @Embed but from a dynamically generated URLRequest or URL String.

var myImage:Image = new Image();
myImage.source = "http://www.domain.com/img/1234.png";
myImage.width = 16;
myImage.height = 16;
myImage.scaleContent = true;

var myButton:LinkButton = new LinkButton();
myButton.label = "Some text"

// This is what I'm trying to figure out.
myButton.setStyle("icon", ???)

I'm probably missing something obvious - I've tried passing in the URL and myImage separately but both throw errors. Stepping into the setStyle() method shows that the code is expecting a Class - so what do I pass in place of the ???

I can't embed the image itself because it's dynamic - the URL is different each time the software runs.

Thanks for any assistance!

A: 

Why not just set the buttonMode of a mx:Image to true then add a click event?

<mx:Image source="{imageSource}" buttonMode="true" click="action()" />

I'm not sure its possible without using an embedded images with a linkButton

This might be worth a read

Edit... in AS3:

var img:Image = new Image();
img.source = "...";
img.buttonMode = true;
img.addEventListenever(MouseEvent.CLICK, funcName);
Chris Klepeis
As I mentioned, we need to create the button programmatically - it's part of a Factory pattern that is manufacturing UI components based on certain states. I'd prefer not to have to create a new MXML component simply for the purpose of binding the image - in the hopes that there is some easy way to set the image through code. But thanks - it's is a reasonable suggestion and I'll try it if a code-based solution isn't possible.
Chris, thanks for the edit. If we only needed the image you're right that this would work fine. But we're looking to have a `LinkButton` to maintain a consistent look-n-feel with other components on the UI and it needs to include the icon, label text and the mouse-over interaction that a `LinkButton` offers out of the box - if we can just get the icon to be populated from a URL...
A: 

I think that instead of trying to set the style, you need to change the child object that holds the icon. You can access it by something like:

Var:Bitmap icon = myButton.getChildByName("upIcon") as Bitmap

It should be easy to replace the bitmapData in there with the one from a Loader.

A: 

<mx:Button id="example" label="Example" icon="{IconUtility.getClass(example, 'http://www.exampledomain.com/image.jpg')}" />

http://blog.benstucki.net/?p=42

Kunal Bhatia
A: 

If memory serves, you'll want to use button.setStyle('icon', img), where img is an image loaded via Loader class.

mway