Gwt获取组件 [英] Gwt get Components

查看:109
本文介绍了Gwt获取组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Vertiacal面板对象,并且此对象包含很多单选按钮

所以我可以通过Vertiacal面板对象获取这些radioButton对象。



也许通过迭代或?

  private void initCourse(){
coursePopupPanel.clear( );
VerticalPanel verticalPanel = new VerticalPanel();
coursePopupPanel.setWidget(verticalPanel);
JsArray< JCourse> jCourseArray = JCourse.getList(stringMainData);
for(int i = 0; i< jCourseArray.length(); i ++){
final RadioButton courseRadioButton = new RadioButton(course);
courseRadioButton.setText(jCourseArray.get(i).getName());
courseRadioButton.getElement()。setId(jCourseArray.get(i).getView());
verticalPanel.add(courseRadioButton);

//单选按钮处理程序
courseRadioButton.addClickHandler(新ClickHandler(){
public void onClick(ClickEvent event){
}
} );




$ b我有一个参考coursePopupPanel。但我没有参考垂直面板,所以我可以获得垂直面板sonce的元素,持有引用coursePopupPanel。

解决方案

GWT VerticalPanel是ComplexPanel的子类,它是包含多个子部件的面板的抽象类。在ComplexPanel中(以及由VerticalPanel继承的)是用于获取子窗口小部件的数量,通过索引获取对它们的引用等的方法。你可以像这样构建一个迭代器:

  Iterator< Widget> vPanelWidgets = myVerticalPanel.iterator(); 
while(vPanelWidgets.hasNext()){
Widget childWidget = vPanelWidgets.next();
if(childWidget instanceof RadioButton){
... do stuff
}
}

我倾向于不为其成员查询小部件。这使我跟随我的关于如何显示RadioButton的决定,遵循你的例子。如果您稍后决定在FlexTable的单元格中显示单选按钮以控制垂直和水平布置,该怎么办?要做出这种改变意味着你的小部件迭代器将无法工作。 FlexTable是一个面板,但不是一个ComplexPanel。如果您决定用FlexTable替换VerticalPanel,我上面写的代码将不起作用。



如果采取这种方法,我会保留我的列表相关的小部件(如一组RadioButton)在某种Java集合中。我将该集合传递给我的演示课程,并在那里编写代码来完成布局。通常这是一个UiBinder类,为这些RadioButton提供@UiField(provided = true)。演示者中的代码然后将我传入的Collection的RadioButton元素关联到UiBinder布局中的RadioButton的UiField占位符。所以我所有的布局决定都在UiBinder xml文件中。如果我决定撕掉垂直面板并将其替换为FlexTable,则假设我将事情正确分开,则可能不必触摸一行Java代码。



[其实,我可能会继续决定在表示层中使用RadioButton,特别是在XML文件中。该演示文稿类将在EventBus上触发一条消息,以指示用户通过RadioButton ValueChangeHandler进行了选择,并且我不关心我是否在VerticalPanel中使用了RadioButtons,或者在FlexTable中使用了ToggleButtons。]


I have a Vertiacal panel object and This object contains many radiobuttons

So can i get those radioButton objects through Vertiacal panel object.

Maybe via iteration or ?

private void initCourse() {
    coursePopupPanel.clear();
    VerticalPanel verticalPanel = new VerticalPanel();
    coursePopupPanel.setWidget(verticalPanel);
    JsArray<JCourse> jCourseArray = JCourse.getList(stringMainData);
    for (int i = 0; i < jCourseArray.length(); i++) {
        final RadioButton courseRadioButton = new RadioButton("course");
        courseRadioButton.setText(jCourseArray.get(i).getName());
        courseRadioButton.getElement().setId(jCourseArray.get(i).getView());
        verticalPanel.add(courseRadioButton);

        //handler of course radio buttons
        courseRadioButton.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
            }
        });
    }
}

I have a reference to coursePopupPanel. but i have not reference to vertical panel, so can i get elements of vertical panel sonce holding reference to coursePopupPanel.

解决方案

A GWT VerticalPanel is a subclass of ComplexPanel, an abstract class for Panels that contain more than one child widget. In ComplexPanel (and so inherited by VerticalPanel) are methods for getting the number of child widgets, getting references to them by index, and so on. You could build an iterator something like this:

Iterator<Widget> vPanelWidgets = myVerticalPanel.iterator();
while (vPanelWidgets.hasNext()){
  Widget childWidget = vPanelWidgets.next();
  if (childWidget instanceof RadioButton) {
    ...do stuff
  }
}

I tend not to query a widget for its members. That ties me to the decisions I made about how to display the RadioButtons, following your example. What if you decide later to display your radio buttons in the cells of a FlexTable in order to control vertical and horizontal arrangement? To make that change means your widget iterator won't work. FlexTable is a Panel but not a ComplexPanel. The code I wrote above won't work if you decide to replace the VerticalPanel with a FlexTable.

If was to take something like this approach, I would keep my lists of related widgets (like a group of RadioButtons) in some sort of Java Collection. I pass that Collection to my presentation class, and inside there I write the code to do the layout. Usually that's a UiBinder class, with "@UiField(provided = true)" for these RadioButtons. The code in the presenter then associates the RadioButton elements of the Collection I passed in to the UiField placeholders for the RadioButtons in the UiBinder layout. So all my layout decisions are actually in the UiBinder xml file. If I decide to rip out my Vertical Panel and replace it with a FlexTable, I might not have to touch a single line of Java code, assuming I separated things out correctly.

[Actually, I would probably keep my decision to use RadioButtons inside the presentation layer, and inside the XML file in particular. That presentation class would fire a message on the EventBus to indicate the user had made a selection via a RadioButton ValueChangeHandler, and I wouldn't care if I used RadioButtons in a VerticalPanel or ToggleButtons in a FlexTable.]

这篇关于Gwt获取组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆