2012-06-06 99 views
0

我有一個GWT選項卡面板,並希望在某個事件(例如按鈕單擊)在另一個選項卡中發生時重新加載單個選項卡。有沒有辦法做到這一點? 另一種可能性是在選擇該選項卡時執行某些代碼(例如,向選項卡添加新元素)。 任何幫助將非常感激,我堅持了一段時間了。GWT:重新加載選項卡面板中的單個選項卡

爲了使問題更具體,我在下面提供了一些代碼。

我有我的代碼組織在屏幕上,有一個主屏幕,啓動選項卡面板。並且每個選項卡都有獨立的屏幕。

,主屏幕的簡化代碼:

public class HomeScreen extends Composite{ 

    public HomeScreen() { 

    TabPanel tabPanel = new TabPanel(); 
    FlowPanel flowpanel; 

    flowpanel = new FlowPanel(); 
    ProfileTabScreen profileTabScreen = new ProfileTabScreen(); 
    flowpanel.add(profileTabScreen); 
    tabPanel.add(flowpanel, "Profile"); 

    flowpanel = new FlowPanel(); 
    GroupsTabScreen groupsTabScreen = new GroupsTabScreen(); 
    flowpanel.add(groupsTabScreen); 
    tabPanel.add(flowpanel, "Groups"); 

    initWidget(tabPanel); 
    } 
    } 

代碼的標籤頁面,該我想要啓動重裝:

private VerticalPanel groupPanel = new VerticalPanel(); 
private Button newGroupButton = new Button("New group"); 

    public GroupsTabScreen() {  

    newGroupButton.addClickHandler(new ClickHandler(){ 
     public void onClick(ClickEvent event) { 
      createNewGroup(); 
     } 
    }); 

      groupPanel.add(newGroupButton); 

    initWidget(groupPanel); 
} 

代碼必須被重新加載標籤屏幕:

private VerticalPanel profilePanel = new VerticalPanel(); 
private Label label = new Label("No groups yet."); 

    public ProfileTabScreen() {  

      profilePanel.add(label); 
    initWidget(profilePanel); 
} 

因此,讓我們想象我只是想更改profileTab中的標籤文本(whi實際上它會是ListBox和其他元素),當newGroupButton在groupTab中被點擊時。

正如我所說,每次重新加載整個profileTab被選中也是可以接受的。

回答

0

對於按鈕單擊可以使用鼠標或鍵監聽器。將這樣一個監聽器添加到標籤面板並讓它調用一個函數,讓我們稱之爲「重建」。

重建函數應該能夠訪問您要更新並重新計算值的選項卡中的元素。

感謝您的代碼示例。如果主屏幕上有它自己的類,則應在初始化它之前定義它所使用的元素。只是一個簡單的例子:

public class HomeScreen extends Composite{ 

    private TabPanel tabPanel; 
    private FlowPanel profileTabScreenPanel; 
    private FlowPanel groupsTabScreenPanel; 

    public HomeScreen() {   

     tabPanel = new TabPanel(); 

     profileTabScreenPanel = new FlowPanel(); 
     ProfileTabScreen profileTabScreen = new ProfileTabScreen(); 
     profileTabScreenPanel.add(profileTabScreen); 
     tabPanel.add(flowpanel, "Profile"); 

     groupsTabScreenPanel = new FlowPanel(); 
     GroupsTabScreen groupsTabScreen = new GroupsTabScreen(); 
     groupsTabScreenPanel.add(groupsTabScreen); 
     tabPanel.add(flowpanel, "Groups"); 

     initWidget(tabPanel); 
    } 
} 

的好處是,每次你實現處理器能夠直接訪問特定的元素,這意味着你可以訪問eleements或重置。

此外,您應該將Getters和setter廣告給您的TabsScreen-Elements,以便您不能訪問標籤。

例如,你可以在你的createNewGroup()做到這一點 - 功能則:

profileTabScreenPanel.getLabel().setText("New Text"); 
+0

我不確定如何將單擊偵聽器添加到選項卡面板以及如何從選項卡面板訪問選項卡中的元素。爲了使我的問題更具體,我添加了一些簡化的程序代碼。 – april

+0

我編輯了我的以及:-) – Corsair

+0

感謝您的回覆。在使用它們並使用getter和setter之前定義元素的想法非常有用。但我無法讓它與你所提示的例子一起工作:profileTabScreenPanel.getLabel()。setText(「New Text」);另一方面,它的工作原理如下:HomeScreen.getProfileTabScreen()。getLabel()。setText(「some text」);我添加了兩個getter方法,一個在HomeScreen類中,另一個在ProfileTabScreen類中。 – april

0

骯髒的答案是打造您要更新的主頁屏幕上,並把它傳遞到這兩個選項卡的標籤,像這樣:

public class HomeScreen extends Composite { 
    public HomeScreen() { 
     TabPanel tabPanel = new TabPanel(); 
     FlowPanel flowpanel; 
     Label labelToUpdate = new Label("No Groups yet"); 

     flowpanel = new FlowPanel(); 
     ProfileTabScreen profileTabScreen = new ProfileTabScreen(labelToUpdate); 
     flowpanel.add(profileTabScreen); 
     tabPanel.add(flowpanel, "Profile"); 

     flowpanel = new FlowPanel(); 
     GroupsTabScreen groupsTabScreen = new GroupsTabScreen(labelToUpdate); 
     flowpanel.add(groupsTabScreen); 
     tabPanel.add(flowpanel, "Groups"); 

     initWidget(tabPanel); 
    } 
} 

public class GroupsTabScreen { 
    private VerticalPanel groupPanel = new VerticalPanel(); 
    private Button newGroupButton = new Button("New group"); 
    private final Label labelToUpdate; 

    public GroupsTabScreen(Label label) { 
     this.labelToUpdate = label;  

     newGroupButton.addClickHandler(new ClickHandler(){ 
       public void onClick(ClickEvent event) { 
        createNewGroup(); 
        labelToUpdate.setText("A group has been created"); 
       } 
     }); 

     groupPanel.add(newGroupButton); 

     initWidget(groupPanel); 
    } 
} 

public class ProfileTabScreen { 
    private VerticalPanel profilePanel = new VerticalPanel(); 
    private final Label labelToUpdate; 

    public ProfileTabScreen(Label label) {  
     this.labelToUpdate = label; 
     profilePanel.add(labelToUpdate); 
     initWidget(profilePanel); 
    } 
} 

更漂亮的解決方案是在類之間使用事件總線來觸發和檢測事件更改。請參閱How to use the GWT EventBus

+0

感謝jonas,我認爲事件總線也可以解決這個問題! – april

相關問題