访问GWT元素内的小部件

时间:2010-03-17 19:52:48

标签: java gwt

我想从main方法访问GWT中这个文本框内的文本元素(我在这里调用它)

DialogBox aBox =  newCandidatePop.buildNewElecPopup();
    aBox.center();
    aBox.getWidget();

    MiscUiTools.newCandidateHandler(aBox.firstName, aBox.surName);

newCandidateHandler中我想将点击处理程序附加到两个文本框

但是,上面的功能还不行 - 我无法访问aBox.firstName元素,因为它们是静态方法 - 我想知道什么是最佳实践,你会怎样编写这样的代码?

static TextBox firstName = new TextBox();
    static TextBox surName = new TextBox();
    static DialogBox box;

//  public newCandidatePop() {
//      box = buildNewElecPopup();
//  }

    static public DialogBox buildNewElecPopup() {

        DialogBox box = new DialogBox();
        box.setAutoHideEnabled(true);

        box.setText("Add a New Candidate");
        box.setAnimationEnabled(true);
        box.setGlassEnabled(true);

        Grid dialogGrid = new Grid(2, 3);
        dialogGrid.setPixelSize(250 , 125);
        dialogGrid.setCellPadding(10);
        dialogGrid.setWidget(0, 0, new HTML("<strong>First Name</strong>"));
        dialogGrid.setWidget(0, 1, firstName);

        dialogGrid.setWidget(1, 0, new HTML("<strong>Surname</strong>"));
        dialogGrid.setWidget(1, 1, surName);

        box.add(dialogGrid);

    return box;
    }

1 个答案:

答案 0 :(得分:1)

为什么TextBox会保持静态?

public class MyDialogBox extends DialogBox {
  private final TextBox firstName;
  private final TextBox surName;
  public MyDialogBox() {
    firstName = new TextBox();
    surName = new TextBox();

    DialogGrid dialogGrid = new Grid(2, 3);
    // do all your stuff with the grid, add TextBoxes, etc.
    add(dialogGrid);

    setAutoHideEnabled(true);
    // set all the properties of your DialogBox
  }

  public TextBox getFirstNameTextBox() {
    return firstName;
  }
  // same with surName...
}