GWT - GXT - How to get Radio Button Value?

17,557

Solution 1

Create radio

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);

get selected value

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

Solution 2

You need to extend the GWT RadioButton class 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);
     }

}

Default it allows only boolean value. After initializing the radio button you need set the value.

Share:
17,557
user18714
Author by

user18714

Updated on June 05, 2022

Comments

  • user18714
    user18714 over 1 year

    I am using GWT (Google Web Toolkit) 1.5.3 et GXT (ExtJS) 1.2 I just want to create a simple form with some radio buttons generated after a RPC call, to get some values

    Code:

    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);
    

    My problem is I can't set/get radio button value. If I try the setRawValue("xxxxxxx"), I will get some null errors, while setting setValue(boolean) is working but I was expecting getting the radio value and not the label value.

    Any Idea?

  • pbojinov
    pbojinov almost 12 years
    radio.setValue() was just what I was trying to find!