2010-03-17 73 views
0

我想從主方法來訪問這個文本中的文本元素GWT(在我這樣稱呼它)一個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

爲什麼TextBoxes是靜態的?

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... 
} 
+0

好點 - 應該沒有。 謝謝! – malangi