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
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();
}
}
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());
}
});
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With