2008-12-10 64 views
3

我使用GWT(谷歌Web工具包)1.5.3等GXT(ExtJS的)1.2 我只是想創建一個簡單的形式與RPC調用後產生了一些單選按鈕,得到一些價值GWT - GXT - 如何獲得單選按鈕值?

代碼:

final FormPanel simple = new FormPanel(); 
     simple.setFrame(true); 
     simple.setWidth(350); 
     simple.setHeaderVisible(false); 

     DateField date = new DateField(); 
     date.setFieldLabel("Date"); 
     simple.add(date); 

     ListFluxServiceAsync service = (ListFluxServiceAsync) 
     GWT.create(ListFluxService.class); 
     ServiceDefTarget target = (ServiceDefTarget)service; 
     String url = GWT.getModuleBaseURL() + "flux.rpc"; 
     target.setServiceEntryPoint(url); 

     final RadioGroup radioGroup = new RadioGroup("RadioGroup"); 
     radioGroup.setFieldLabel("Flux"); 
     radioGroup.setOrientation(Orientation.VERTICAL); 

     service.getAllFlux(new AsyncCallback<List<FluxModelData>>(){ 

      public void onFailure(Throwable caught) { 
       GWT.log("flux.rpx::onFailure", caught); 
       MessageBox.alert("what?", "onFailure :" + caught.getMessage(), null); 
      } 

      public void onSuccess(List<FluxModelData> result) { 

       Iterator<FluxModelData> it = result.iterator(); 
       while (it.hasNext()){ 
        FluxModelData fmd = it.next(); 
        Radio radio = new Radio(); 
        radio.setName("flux"); 
        radio.setValue(true); 
        //radio.setRawValue("my very long value"); 
        radio.setBoxLabel(fmd.getDescription()); 
        radioGroup.add(radio); 
       } 

       simple.add(radioGroup); 
       simple.layout(); //we need it to show the radio button 

      } 
     }); 



     simple.setButtonAlign(HorizontalAlignment.CENTER); 

     Button button = new Button("Récupérer"); 
     button.addSelectionListener(new SelectionListener<ButtonEvent>(){ 

      @Override 
      public void componentSelected(ButtonEvent ce) { 

       MessageBox.alert("what?", radioGroup.getValue().getRawValue() , null); 

      }}); 

     simple.addButton(button); 


     RootPanel.get().add(simple); 

我的問題是我無法設置/獲得單選按鈕值。 如果我嘗試setRawValue(「xxxxxxx」),我會得到一些空錯誤,而設置setValue(布爾)正在工作,但我期望獲得無線電值,而不是標籤值。

任何想法?

回答

0

使用radioButton.setItemId()和getItemId()來解決它。

0

我使用radio.setAttributeValue()方法來設置單選按鈕的值。

2

創建無線電

Radio radio = new Radio(); 
radio.setBoxLabel("Si"); 
radio.setValue(true); 
radio.setValueAttribute("true"); 

Radio radio2 = new Radio(); 
radio2.setBoxLabel("No"); 
radio2.setValueAttribute("false"); 

RadioGroup radioGroup = new RadioGroup(); 
radioGroup.setFieldLabel("Afecto"); 
radioGroup.add(radio); 
radioGroup.add(radio2); 

得到選擇的值

Boolean b = Boolean.parseBoolean(radioGroup.getValue().getValueAttribute()); 
+0

radio.setValue()正是我試圖找到! – pbojinov 2012-02-07 22:34:36

1

你需要擴展GWT單選按鈕類ex:

public class ExtRadioButton extends RadioButton { 
    public ExtRadioButton(String name, String label) { 
     super(name, label); 
     // TODO Auto-generated constructor stub 
    } 

    public void setValue(String value) 
    { 
     Element span = getElement(); 
     Element input = DOM.getChild(span,0); 
     DOM.setElementAttribute(input,"value",value); 
    } 

} 

默認情況下只允許布爾值。初始化單選按鈕後,您需要設置值。

0

看看這個

Radio radio1 = new Radio(); 
............. 
Radio radio2 = new Radio(); 
............. 

in order to get value you can do as follow 

String value = (radio1.getValue()) ? radio1.getText() : radio2.getText(); 
0

另一種方式來做到這一點是使用radio.setValueAttribute(String)方法。然後你可以使用下面的代碼在RadioGroup中獲得點擊的單選按鈕的設置「值」屬性:

radioGroup.addListener(Events.Change, new Listener<BaseEvent>() { 
    @Override 
    public void handleEvent(BaseEvent be) 
    { 
    final RadioGroup radioGroup = (RadioGroup)be.getSource(); 

    final Radio clickedRadioBtn = radioGroup.getValue(); 

    final String valueAttribute = clickedRadioBtn.getValueAttribute(); // Correct !!! 

    } 
}); 
0
  Radio includeButton = new Radio(); 
     Radio excludeButton = new Radio(); 
     RadioGroup radioGroup = new RadioGroup(); 




      radioGroup.add(includeButton); 
    radioGroup.add(excludeButton); 

      includeButton.setvalue(true)//false