Get Toggle RadioButton user value


Question

In my javafx project code is like that.How to get radioButton String value i need outbut is like that. I will click today, output is print Today as so an

  • Today
  • Yesterday
  • Duration

How to this output from group.selectedToggleProperty().addListener(new ChangeListener<Toggle>()

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.RadioButton;
import javafx.scene.control.Toggle;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

/**
 *
 * @author reegan
 */
public class GetSelectedToggleButton extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) {
        Scene scene = new Scene(new Group());
        stage.setTitle("Toggle Button Sample");
        stage.setWidth(250);
        stage.setHeight(180);
        final ToggleGroup group = new ToggleGroup();
        RadioButton today = new RadioButton("Today");
        today.setToggleGroup(group);
        today.setSelected(true);
        RadioButton yesterday = new RadioButton("Yesterday");
        yesterday.setToggleGroup(group);
        RadioButton duration = new RadioButton("duration");
        duration.setToggleGroup(group);
        group.selectedToggleProperty().addListener(new ChangeListener<Toggle>() {
            @Override
            public void changed(ObservableValue<? extends Toggle> ov, Toggle t, Toggle t1) {
                System.out.println(t1);
                System.out.println(group.selectedToggleProperty().toString());
            }
        });
        HBox hbox = new HBox();
        hbox.getChildren().add(today);
        hbox.getChildren().add(yesterday);
        hbox.getChildren().add(duration);
        ((Group) scene.getRoot()).getChildren().add(hbox);
        stage.setScene(scene);
        stage.show();
    }
}
1
3
1/27/2014 6:18:43 AM

Accepted Answer

To get value of radio button type cast listener object to radio button.

try this..

final ToggleGroup group = new ToggleGroup();
    RadioButton today = new RadioButton("Today");
    today.setToggleGroup(group);
    today.setSelected(true);
    RadioButton yesterday = new RadioButton("Yesterday");
    yesterday.setToggleGroup(group);
    RadioButton duration = new RadioButton("duration");
    duration.setToggleGroup(group);

    group.selectedToggleProperty().addListener(new ChangeListener<Toggle>() {
        @Override
        public void changed(ObservableValue<? extends Toggle> ov, Toggle t, Toggle t1) {

            RadioButton chk = (RadioButton)t1.getToggleGroup().getSelectedToggle(); // Cast object to radio button
            System.out.println("Selected Radio Button - "+chk.getText());

        }
    });
15
1/27/2014 7:03:31 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon