List all the widgets of a page/panel in GWT

早过忘川 提交于 2019-12-25 05:06:19

问题


Is there a programmatic way to read all the widget a GWT page or Panel contains?

I have several error labels in my form, all suffixed by "ErrorLabel" and I'd like to find them and clear them. I can do a list of them, but I figured out I can actually automate that if I had some...

I am using UiBinder.


回答1:


Use recursion method to get all the children of any component.

Steps to follow: (How to read the UiField names?)

  • Add below entry in you gwt.xml

    <inherits name="com.google.gwt.user.Debug"/> 
    
  • Use debugId along with ui:field as shown below in your ui.xml

    <gwt:CheckBox ui:field="myCheckBox" debugId="myCheckBox" />
    
  • Now you can get the Id

    myCheckBox.getElement().getId();
    
  • All the Ids are generated with default prefix gwt-debug- as shown below. If you want then you can remove it.

    gwt-debug-myCheckBox       
    

Here is a utility class:

Note: add more widget and component as per your requirement.

import java.util.Iterator;

import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.Widget;

public class WidgetValidator {

    private static void validateFlexTable(final FlexTable flextable) {
        for (int row = 0; row < flextable.getRowCount(); row++) {
            for (int column = 0; column < flextable.getCellCount(row); column++) {
                WidgetValidator.validateWidget(flextable.getWidget(row, column));
            }
        }
    }

    private static void validatePanel(final Panel panel) {
        final Iterator<Widget> iterator = panel.iterator();
        while (iterator.hasNext()) {
            WidgetValidator.validateWidget(iterator.next());
        }
    }

    public static void validateWidget(final Widget widget) {
        if (widget != null) {
            if (widget instanceof FlexTable) {
                WidgetValidator.validateFlexTable((FlexTable) widget);
            } else if (widget instanceof Panel) {
                WidgetValidator.validatePanel((Panel) widget);
            } else {
                System.out.println(widget.getElement().getId().replace("gwt-debug-", ""));
            }
        }
    }

}



回答2:


The simplest solution is to add a class name to all the labels - for example, "errorLabel", and add the following line to your CSS file:

.hide .errorLabel {display: none}

Then, if you need to hide/show all of these labels, you can do it with a single line of code:

parentPanel.addStyleName("hide");
parentPanel.removeStyleName("hide");



回答3:


Something like that

private void myFunction(RootPanel panel) {
for (Widget w : panel) {
    if(w instanceof Label){
       //do something
    }
}
}


来源:https://stackoverflow.com/questions/22864164/list-all-the-widgets-of-a-page-panel-in-gwt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!