How to dynamically change pushbutton icon?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

How to dynamically change pushbutton icon?

JohnRodey
Having trouble changing the icon for a pushbutton in the java code.

In the wtkx I can do this:
<content:ButtonData icon="@myimage.png"/>

But in the java code I cannot do this:
((ButtonData)someButton.getButtonData()).setIcon("@mysecondimage.png");

I get: iconURL is null

How am I supposed to define it here?

Thanks!
Reply | Threaded
Open this post in threaded view
|

Re: How to dynamically change pushbutton icon?

JohnRodey
Scratch that,  I was able to use the full relative path:
((ButtonData)someButton.getButtonData()).setIcon("com/my/path/mysecondimage.png");
Reply | Threaded
Open this post in threaded view
|

Re: How to dynamically change pushbutton icon?

Greg Brown
Administrator
That works. You can also use a URL. The '@' symbol is translated to a URL internally by the WTKXSerializer. It allows you to load an image using a path that is relative to the current WTKX file.

In Java, you can do something similar using Class#getResource(). For example, if the image file is located in the same directory as your class file, you could do something like this:

((ButtonData)someButton.getButtonData()).setIcon(getClass().getResource("mysecondimage.png"));

This approach is a little more resilient to refactoring, since it doesn't rely on a hard-coded path.

On May 13, 2010, at 6:20 PM, JohnRodey wrote:

>
> Scratch that,  I was able to use the full relative path:
> ((ButtonData)someButton.getButtonData()).setIcon("com/my/path/mysecondimage.png");
> --
> View this message in context: http://apache-pivot-users.399431.n3.nabble.com/How-to-dynamically-change-pushbutton-icon-tp815958p815983.html
> Sent from the Apache Pivot - Users mailing list archive at Nabble.com.