stevek stevek - 2 months ago 6
Java Question

JavaFX ToggleButton, is there a way to pass if its clicked as a "status"?

Is there a way for me to pass the status of toggle button(s) as a argument/parameter? I would like for it to work like this scenario:

1) Press a button.
2) Press GO!
3) Depending on which button is pressed, it is passed so there are different outcomes when I press GO!

Thanks!

yab yab
Answer Source

here's some code for reference, if it is not what you want, let me know.

public class Foo extends Application{
  @Override
  public void start(Stage primaryStage) throws IOException{
    ToggleGroup group = new ToggleGroup();

    ToggleButton tb1 = new ToggleButton("ToggleButton 1");
    ToggleButton tb2 = new ToggleButton("ToggleButton 2");
    ToggleButton tb3 = new ToggleButton("ToggleButton 3");

    tb1.setToggleGroup(group);
    tb2.setToggleGroup(group);
    tb3.setToggleGroup(group);

    Button btn = new Button("GO");
    btn.setOnAction(new EventHandler<ActionEvent>(){
      @Override
      public void handle(ActionEvent event){
        Toggle selectedTogger = group.getSelectedToggle();
        if(selectedTogger == tb1){
          //outcome 1 (ToggleButton1 selected)

        }else if(selectedTogger == tb2){
          //outcome 2 (ToggleButton2 selected)

        }else if(selectedTogger == tb3){
          //outcome 3 (ToggleButton3 selected)

        }else{
          //outcome 4 (No ToggleButton selected)

        }
      }
    });

    VBox vbox = new VBox();
    vbox.getChildren().addAll(tb1, tb2, tb3, btn);

    primaryStage.setScene(new Scene(vbox));
    primaryStage.show();
  }

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