Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GWT Image load handler

Tags:

java

onload

gwt

GWT: 2.6.1
I would like to execute an action when an image is loaded (image src).

I tried this, but the onLoad event is never fired :

    final Image img = new Image();
    img.addLoadHandler( new LoadHandler()
    {

        @Override
        public void onLoad( LoadEvent event )
        {
            //action
        }
    } );

    img.setUrl( "/image.png" );

Then I tried this :

    final Image img = Image.wrap( Document.get().createImageElement() );
    img.addLoadHandler( new LoadHandler()
    {

        @Override
        public void onLoad( LoadEvent event )
        {
            //action
        }
    } );

    img.setUrl( "/image.png" );

And it worked... I don't understand why the first code do nothing.

Important: In this two examples, I don't put the Image object into the dom (= Image isn't attached).

In this question, it seems that the cause is that the Image isn't attached. But in my second sample code, the image isn't attached too ? right ?

like image 469
puglic Avatar asked Jul 04 '26 22:07

puglic


1 Answers

If you take a look into documentation of the wrap() method, it says This element must already be attached to the document. . So, when you actually check source of the wrap() it calls onAttach() which actually registers event handlers to the DOM element. So the difference is that Image object thinks element is attached while it is not in that state.

Now, on entrance of the wrap() there is assertion that checks the condition is met. Though you need to enable assertions to have them effective.

like image 176
okrasz Avatar answered Jul 07 '26 12:07

okrasz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!