Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Toggle RadioButton user value

Tags:

javafx

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();
    }
}
like image 419
Reegan Miranda Avatar asked Dec 01 '22 17:12

Reegan Miranda


2 Answers

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

        }
    });
like image 197
Anshul Parashar Avatar answered Dec 04 '22 05:12

Anshul Parashar


If your toggle group consists of RadioButtons, you can find the value of the selected Radio Button by

  String supplementation = ((RadioButton)supplememntation_toggle_group.getSelectedToggle()).getText();
like image 33
Martin Mbae Avatar answered Dec 04 '22 07:12

Martin Mbae