且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

GWT动态更改图像

更新时间:2023-12-03 11:04:28

我认为这特别是 applyTo 是for。为什么你不这样做:

  / *假设id为imageId的元素是真实图像
元素并且未定义:* /
myImageBundle.icon()。applyTo(Image.wrap(Document.get()。getElementById(imageId)));

//编辑后使用Image.wrap

以上应用您在图标()中定义的图像为ID为imageId的图像


I want to change dynamically an image by using the ID and a Ressource Bundle.

Element changedImage;
 /* imageID is the Id for the previous image */
 changedImage = Document.get().getElementById("imageId");
 /* And myImageBundle is the resource Bundle and icon the new image*/
 changedImage.setInnerHTML(myImageBundle.icon().getHTML());

But nothing happens , it's the same image . Did i miss something ?

Thanks a lot for answer(s) or suggestions(s).

-------- Solution ---------------

I've found a solution , it works properly, but somehow i think it's not the best one . Everything is on the next function :

    private void switchImage(  AbstractImagePrototype imageBundled) {

    String newStyleFromImageBundle, value;
    value = "style=";

    newStyleFromImageBundle = extractStyle(value ,imageBundled.getHTML());

    Element e = Document.get().getElementById("imageHeaderLogo");       
    // removing the current style
    e.removeAttribute("style");     
    // setting the new style with the  previous one retrieved from the image bundled's html
    e.setAttribute("style",newStyleFromImageBundle );


}

Hope it helps, feel free to tell if there's a better way to do it or this the worst you 've ever seen .. :-) .

I think this is specifically what applyTo is for. Why don't you just do the following:

/*assuming that the element with id "imageId" is a real image 
element and is not undefined:*/
myImageBundle.icon().applyTo(Image.wrap(Document.get().getElementById("imageId")));

//editted to use Image.wrap

The above should apply the image you have defined in icon() to the image with id "imageId"