2013-01-07 74 views
0

我有Back/Forward鍵(功能並不重要,只想讓我的頭周圍和縮小GWT頁面交換面板)GWT視圖導航基本的例子

NavigationButtonsPanel

導航面板
public class NavigationButtonsPanel extends HTMLPanel 
     { 
      private Button btnA = new Button("Go back"); 
      private Button btnB = new Button("Go forward"); 

      public NavigationButtonsPanel() 
      { 
       super("Navigation Panel"); 
       add(btnA); 
       add(btnB); 
      } 

      public void setBackHandler(ClickHandler handler) 
      { 
       btnA.addClickHandler(handler); 
      } 

      public void setForwardHandler(ClickHandler handler) 
      { 
       btnB.addClickHandler(handler); 
      } 
     } 

我然後有3個基本面板,AB和C,其使用此。我從A-B-C導航回到B-A可以正常工作。他們去之前,面板B.在這種情況下PanelB一個標籤 我現在模擬用戶不斷變化的東西。 我的最終應用程序顯然會比這更復雜,但這是典型的應用程序如何工作。您可以通過同一面板或不同面板中的用戶交互來修改面板中的某些字段,加載新面板,進行更多交互,加載不同面板等? 我只希望用戶因此只有一個路徑在一定面板使用了非常明確的導航路徑。

紅砂糖

public class PanelA extends HTMLPanel 
{ 
    private static PanelA panel;    
    private PanelA() 
    { 
     super("Panel A"); 
     final RootPanel rootPanel = RootPanel.get(); 

     NavigationButtonsPanel btnPanel = new NavigationButtonsPanel(); 
     btnPanel.setForwardHandler(new ClickHandler() { 
      public void onClick(ClickEvent event) 
      { 
       rootPanel.clear(); 
       PanelB.setMessageFromPreviousPage("Message set in Panel AAAAA"); 
       rootPanel.add(PanelB.getInstance()); 
      } 
     }); 

     add(btnPanel); 
    } 

    public static PanelA getInstance() 
    { 
     if (panel == null) 
     { 
      panel = new PanelA(); 
     } 
     return panel; 
    } 
} 

PanelB

public class PanelB extends HTMLPanel 
{ 
    private static PanelB panel; 
    private static String messageFromPreviousPage; 

    private PanelB() 
    { 
     super("Panel B"); 
     final RootPanel rootPanel = RootPanel.get(); 

     Label messageLabel = new Label(messageFromPreviousPage); 


     NavigationButtonsPanel btnPanel = new NavigationButtonsPanel(); 
     btnPanel.setBackHandler(new ClickHandler() { 
      public void onClick(ClickEvent event) 
      { 
       rootPanel.clear(); 
       rootPanel.add(PanelA.getInstance()); 
      } 
     }); 

     btnPanel.setForwardHandler(new ClickHandler() { 
      public void onClick(ClickEvent event) 
      { 
       rootPanel.clear(); 
       rootPanel.add(PanelC.getInstance()); 
      } 
     }); 

     add(btnPanel); 
     add(messageLabel); 
    } 

    public static PanelB getInstance() 
    { 
     if (panel == null) 
     { 
      panel = new PanelB(); 
     } 
     return panel; 
    } 

    public static void setMessageFromPreviousPage(
      String message) 
    { 
     messageFromPreviousPage = message; 


} 
    } 

PanelC

public class PanelC extends HTMLPanel 
     { 
      private static PanelC panel;    
      private PanelC() 
      { 
       super("Panel C"); 
       final RootPanel rootPanel = RootPanel.get(); 

       NavigationButtonsPanel btnPanel = new NavigationButtonsPanel(); 
       btnPanel.setBackHandler(new ClickHandler() { 
        public void onClick(ClickEvent event) 
        { 
         rootPanel.clear(); 
         PanelB.setMessageFromPreviousPage("Message set in Panel CCCCCC"); 
         rootPanel.add(PanelB.getInstance()); 
        } 
       }); 

       add(btnPanel); 
      } 

      public static PanelC getInstance() 
      { 
       if (panel == null) 
       { 
        panel = new PanelC(); 
       } 
       return panel; 
      } 
     } 

回答

0
Any idea why Panel B does not update its UI? 
01第一時間時使用 PanelB.getInstance() PanelB實例創建

因爲,然後messageLabel創建,它也設定值。而您在更改消息後第二次從PanelC調用同一方法時,此時它使用的是PanelB的相同實例,而不是新對象。並根據您的編碼,您正在更改messageFromPreviousPage變量的值而不是標籤文本。所以你沒有在UI上找到任何改變。

如果要更改標籤文本,則將messageLabel聲明爲globel變量靜態標籤和更改標籤。即

private static Label messageLabel; 
private PanelB() 
{ 
//your code 
} 
public static PanelB getInstance() 
{ 
    if (panel == null) 
    { 
     panel = new PanelB(); 
    } 
    else 
    { 
     messageLabel.setText(messageFromPreviousPage); //Require to set Text Again 
    } 
    return panel; 
}