2012-08-27 61 views
2

我想在GWT中製作一個動態TabPanel。我希望TabPanel中的文本根據用戶在文本框中輸入的內容進行更改。例如,輸入「Tab one」會將標籤文本更改爲「Tab one」。但是,我似乎無法找到更改選項卡名稱的方法。 getTitle()只是返回標題,而不是實際的文本。有誰知道如何做到這一點?更改GWT TabPanel名稱

一種方法可能是刪除選項卡並使用相同的內容重新創建它,但如果可能,我真的很想避免這種情況。 謝謝。

+3

TabPanel.getTabBar()然後TabBar.setTabText()? – onnoweb

+0

完美!我只是在尋找TabPanel.setText()或TabPanel.getTabBar()。getTab()。setText()。 –

回答

2

那麼「onnoweb」已經給出了正確的答案,但在這裏你有一個演示的例子。

package stefank.client; 

import com.google.gwt.core.client.EntryPoint; 
import com.google.gwt.dom.client.Style.Unit; 
import com.google.gwt.event.dom.client.ClickEvent; 
import com.google.gwt.event.dom.client.ClickHandler; 
import com.google.gwt.user.client.ui.Button; 
import com.google.gwt.user.client.ui.HTML; 
import com.google.gwt.user.client.ui.RootPanel; 
import com.google.gwt.user.client.ui.TabLayoutPanel; 

public class _02_GWTAnimation implements EntryPoint { 

    public void onModuleLoad() { 

     // Create a tab panel 
     final TabLayoutPanel tabPanel = new TabLayoutPanel(2.5, Unit.EM); 
     tabPanel.setHeight("100px"); 
     tabPanel.setAnimationDuration(1000); 
     tabPanel.getElement().getStyle().setMarginBottom(10.0, Unit.PX); 

     // Add a home tab 
     String[] tabTitles = {"hello", "world"}; 
     HTML homeText = new HTML("Lorem ipsum"); 
     tabPanel.add(homeText, tabTitles[0]); 


     // Add a tab 
     HTML moreInfo = new HTML("Lorem ipsum"); 
     tabPanel.add(moreInfo, tabTitles[1]); 

     // Return the content 
     tabPanel.selectTab(0); 
     tabPanel.ensureDebugId("cwTabPanel"); 

     RootPanel.get().add(tabPanel); 

     Button changeText = new Button("change Text"); 
     changeText.addClickHandler(new ClickHandler() { 

      @Override 
      public void onClick(ClickEvent event) { 
       tabPanel.setTabText(0, "new Title");     
      } 
     }); 
     RootPanel.get().add(changeText); 

    } 
}