Search code examples
javaclassgwtuibinder

How to check currently added class on ROOTPanel in GWT


I am using two uibinder class and it added to the ROOTPanel. Suppose i have two class ABC and XYZ and if i added it on ROOTPanel then i want to know which class is currently added on the ROOTPanel.

Here is the code

So ABC class is added on the ROOTPanel using id "panel"

if(i==1) {
   ROOTPanel.get("panel").add(new ABC());
} else {
   ROOTPanel.get("panel").add(new XYZ());
}

Now i want that which class is added on "panel" id

if(//some condition which return the true or false for ABC class is added or not) {
   // to do something
}

Solution

  • You can check the index of the added Widget using method getWidgetIndex, and put the condition that index != -1 because according the documentation of this method:

    Gets the index of the specified child widget.

    Specified by: getWidgetIndex(...) in IndexedPanel
    Parameters:child the widget to be found
    Returns:the widget's index, or -1 if it is not a child of this panel

    so your code becomes:

    ABC abc = new ABC();
    XYZ xyz = new XYZ();
    if(i==1) {
       ROOTPanel.get("panel").add(abc);
    } else {
       ROOTPanel.get("panel").add(xyz);
    }
    if(ROOTPanel.get("panel").getWidgetIndex(abc) != -1) {
       // You know ABC was added
    }
    

    If you do not want to instantiate the ABC and XYZ objects before adding then you must save the index of the added widget and then use method .getWidget(index) and check the class type of the returned widget like:

    Widget widget = ROOTPanel.get("panel").getWidget(index);
    
        if(widget instanceof ABC){
        // do something
        }