2012-03-26 81 views
0

您好我使用GWT和我有了下面的事件處理程序一個com.smartgwt.client.widgets.Button:更改按鈕標題問題

 Button viewCommentsButton = new Button("View "); 
     viewCommentsButton.addClickHandler(new ClickHandler() { 

      @Override 
      public void onClick(ClickEvent event) { 
       if (!childrenVisible) { 
        addChildren(); 
        getParent().setTitle("Close"); 
       } else { 
        removeChildren(); 
        getParent().setTitle("View"); 
       } 
      } 

     }); 

正如你可以看到我試過的getParent()的setTitle()方法但沒有效果。如果工作正常,所以我想我不能得到我的按鈕對象的引用,但代碼編譯和getParent返回一個小部件,所以很可能是我的按鈕。

但是,addChildren和removeChildren方法正常工作,但我的按鈕始終都有初始標題。任何想法爲什麼?希望這是有道理的。

歡迎任何建議。謝謝。

回答

2

如果你想設置標題上viewCommentsButton,叫viewCommentsButton.setTitle()

如果您嘗試設置按鈕中的文本,請撥打viewCommentsButton.setText()

不論是出於你必須標記按鈕,最後 - 與final Button viewCommentsButton = ...

聲明它的getParent()的背景是混亂。 getParent(),您使用它的方式將返回您定義所有這一切的小部件的父級,而不是viewCommentsButton的父級,並且絕對不是viewCommentsButton本身。

+0

我會在7分鐘內接受。我有這個選擇開始,但我不得不聲明變量全局或最終,我想給它一個鏡頭。 +1 – Fofole 2012-03-26 13:18:56

1

您應該使用的setText

是的setTitle的 「提示」

+0

我沒有爲我的按鈕oject使用setText方法可以。 – Fofole 2012-03-26 13:15:42

+0

奇怪的http://google-web-toolkit.googlecode.com/svn/javadoc/2.3/com/google/gwt/user/client/ui/Button.html記錄它 – 2012-03-26 13:20:15

+0

您是對的。但我使用com.smartgwt.client.widgets.Button而不是com.google.gwt.user.client.ui.Button,它們有些不同。然而,嘗試幫助+1。 – Fofole 2012-03-26 13:24:26

1

使你的按鈕成爲一個類變量,而不是一個方法變量,並且你可以在點擊處理程序中使用它(引用它)。

例如:

viewCommentsButton = new Button("View "); //viewCommentButton is the private member. 
    viewCommentsButton.addClickHandler(new ClickHandler() { 

     @Override 
     public void onClick(ClickEvent event) { 
      if (!childrenVisible) { 
       addChildren(); 
       viewCommentButton.setTitle("Close"); 
       viewCommentButton.setText("Close"); 
      } else { 
       removeChildren(); 
       viewCommentButton.setTitle("View"); 
       viewCommentButton.setText("View"); 
      } 
     } 

    }); 
+0

+1你說得對,雖然我主要擔心的是如果getParent()讓我引用我的按鈕或不。 – Fofole 2012-03-26 13:25:43

+0

我想像一些人提到的那樣,它指的是父窗口小部件,通常是窗格或輸入,而不是按鈕。 – biplav 2012-03-26 13:28:00