views:

322

answers:

2

I can use removeFromParent to get rid of something on the RootPanel from a class that doesnt have onModuleLoad in it. However, can you add a widget to the RootPanel from another class? I cannot figure out how to do so. So for example:

public classA extends EntryPoint{

public void onModuleLoad(){

MyPanel panel = new MyPanel();
RootPanel.get("a").add(panel);

}

}

Then inside the MyPanel class inside an onclick actions I would want to do this:

Object s = event.getSource();
Button sButton = (Button)s;
sButton.getParent().removeFromParent();

this works, but now we have remove panel from from the rootpanel, and I want to replace it with a new panel. How do I do that from this other class. I am not sure if I have to access RootPanel, which seems to be not possible, or if I need to have another entryPoint which doesn't seem to make sense. Any advice would be appreciated.

+1  A: 

Add this code in your MyPanel class.I haven't tried this code.But it should work.Call the buttonClicked method from onClick method

private void buttonCLicked(){
     for(int i=0;i<RootPanel.get("a").getWidgetCount();i++){
         if(RootPanel.get("a").getWidget(i) .equals(this)){
             RootPanel.get("a").remove(i);
             RootPanel.get("a").add(newPanel);
             break;
        }
     }
 }
BlackPanther
A: 

For some reason when I started using gwt I thought that RootPanel was inaccessible from outside EntryPoint.onModuleLoad() but it is as long as you import RootPanel. I would be interested to know why you thought this because I don't remember why I once thought the same thing. Either way you have two options:

One is just:

Object s = event.getSource();
Button sButton = (Button)s;
sButton.getParent().removeFromParent();
RootPanel.get("a").add(nextPanel);

The other is if you actually wanted to access something that was inaccessible (which like I mentioned RootPanel is perfectly accessible) you could just create a method to access it. So if you truley couldn't access RootPanel outside of onModuleLoad() you could do something like the following:

RootPanel root = null;

public static RootPanel getRootPanel(){
    return root;
    }

public void onModuleLoad(){

root = RootPanel.get("a");
...
        }

and then in whatever class you would like you could just say:

ClassA.getRootPanel().add(Whatever Widget);
Atreides