tags:

views:

314

answers:

3

I have Buttons attached to elements on the modules entrypoint html page using RootPanel.get("foo").add(button). If I subsequently create a LayoutPanel and attach it using RootLayoutPanel.get.add(layoutpanal) then the buttons cannot be clicked. This is all fine. If I then try and remove the layoutpanel or clear the RootLayoutPanel the buttons still cannot be clicked.

Any ideas how to clear this? Have I missed a step or should you simply never try and get back to using a page's RootPanel if you have used a RootLayoutPanel?

Sample code:

public void onModuleLoad(){

    final LayoutPanel lp1=new LayoutPanel();

    ClickPanel ping=new ClickPanel("Ping");
    ping.getElement().getStyle().setBackgroundColor( "#fdd" );
    ping.addClickHandler( new ClickHandler(){
        @Override
        public void onClick( ClickEvent event ){
            Window.alert( "Ping!!!" );
            //lp1.removeFromParent();
            //RootLayoutPanel.get().remove(lp1);
            //RootLayoutPanel.get().removeFromParent();
            RootLayoutPanel.get().clear();
        }
    } );

    ClickPanel bong=new ClickPanel("Bong");
    bong.getElement().getStyle().setBackgroundColor( "#ddf" );
    bong.addClickHandler( new ClickHandler(){
        @Override
        public void onClick( ClickEvent event ){
            Window.alert( "Bong!!!" );
        }
    } );

    lp1.add( ping );
    lp1.setWidgetLeftWidth( ping, 100, Style.Unit.PX, 500, Style.Unit.PX );
    lp1.setWidgetTopHeight( ping, 100, Style.Unit.PX, 500, Style.Unit.PX );

    lp1.add( bong );
    lp1.setWidgetLeftWidth( bong, 50, Style.Unit.PCT, 600, Style.Unit.PX );
    lp1.setWidgetTopHeight( bong, 50, Style.Unit.PCT, 200, Style.Unit.PX );

    Button b=new Button("Click Me");
    b.addClickHandler( new ClickHandler(){
        @Override
        public void onClick( ClickEvent event ){
            RootLayoutPanel.get().add( lp1 );
        }
    } );
    RootPanel.get("button1").add( b );
}

ClickPanel is simply overrides HTMLPanel implementing HasClickHandelers. Clicking "Click Me" opens the layout panel. Clicking the panel ping gets rid of the layout panel, but the button "Click Me" cannot be clicked. I've tried various options.

Update

Calling RootPanel.get().remove(RootLayoutPanel.get()) removes the RootLayoutPanel clearing the LayoutPanel and allowing widgets on the RootPanel to be clickable. However it does causes something to get screwed up because any subsequent call to RootLayoutPanel.get.add() throws an IndexOutOfBoundsException. So this is not the full answer.

A: 

How about RootPanel.get().remove(lp1)? (where lp1 is the LayoutPanel from the code above)

Igor Klimer
No, that does nothing. I gave it a try, but was not really expecting it to work.
kerrr
+1  A: 

Second StackOverflow question, second answer by myself. Is that bad?

So, the first call to RootLayoutPanel.get() does two things; creates a RootLayoutPanel object, keeping a singleton reference to itself, and adds itself to the RootPanel by calling RootPanel.get().add().

You can remove it using RootPanel.remove(RootLayoutPanel.get()) however this will still leave the singleton reference to RootLayoutPanel which will be returned if you call RootLayoutPanel.get(). Since this is not attached to anything you can't add anthing too it without throwing an IndexOutOfBoundsException.

To work around this you can manually reattach the RootLayoutPanel to the RootPanel with RootPanel.get().add(RootLayoutPanel.get()). Calling this even if the RootLayoutPanel is already attached doesn't seem to do any harm... as far as I can tell.

kerrr
I add the exact same problem and used that solution. It works. However, I did not find it documented anywhere.
Philippe Beaudoin
A: 

Why do you use RootPanel at all?

You should try

RootLayoutPanel.get("foo").add(button);
RootLayoutPanel.get.add(layoutpanal);

RootLayoutPanel attaches itself to the document body (Although in this case it should attach to the foo element). As much as I know it takes up all the available space on the screen (or to be more precise - it takes up all the space in the browser window).

This way you will be able to use the button and have the layoutpanel.

markovuksanovic
I'm not sure what you expect to happen with that code, but there is no such method RootLayoutPanel.get("foo").
kerrr
As to the question "Why do you use RootPanel at all?" It's existing code that controls the layout of the page. I'm adding somethign new and want it to take over the whole screen then go away after I'm done with it.
kerrr
you're right - rootLayoutPanel.get("foo") doesn't exist. I've checked in the docs. I guess that the problem is because RootLayoutPanel attaches itself to the body element. I'll have a look in the code and see what happens exactly...
markovuksanovic
Have you opened an issue for this at the gwt project page?
markovuksanovic
It seems that this is happening because of calling add function right after remove. I have debugged and have noticed that if I just put breakpoints when adding a new widget (right after removing), and wait a sec before resuming, it works fine. My guess would be that it takes some time till the change is properly propagated.
markovuksanovic
This doesn't mesh very well with my understanding of the code described in my answer. Certainly "waitign a sec" is not a practical answer, even if it did work.
kerrr
Yeah, i'm very well aware that this is not a practical answer - and is even less of a practical solution. I am starting to think that this may only happen in hosted mode.. Will check that option as well...
markovuksanovic
could you post a more complete sample... I have just found out that IndexOutOfboudnsException is thrown if you try to add the same instance of a widget two or more times to RootLayoutPanel (From what I have seen in the code this should happen whenever you try to add (two or more times) the same widget - to be more precise, the instance of the same widget - to some other widget).
markovuksanovic
It would be great if you sent the whole code with which you get the error so that I can debug it - I believe I'm on the right track to find what exactly the problem is.
markovuksanovic
erm... Not sure what you're trying to do. I'm quite satisfied that the answer I gave explains what's happening and the workaround required to resolve it. Please comment on the answer if you have any questions about it.
kerrr
I'm trying to figure out why this is happening under the hood so that I could try to fix it. Anyway I'll open an issue because of the problem when adding the widget twice to the same parent - IMHO, this should be handled properly, not with an unhandled index out of bounds exception.
markovuksanovic