0

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!

stevek
  • 113
  • 1
  • 2
  • 7
  • 2
    It's not really clear what you're asking. Can't you just check the `selected` state of the toggle buttons in the event handler for the "GO" button? Or if the toggle buttons are all in the same toggle group, just check the value of `toggleGroup.getSelectedToggle()`? – James_D Jun 20 '17 at 16:19

1 Answers1

1

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);
  }
}
yab
  • 267
  • 3
  • 13
  • Thanks, this is the answer I was looking for. "Toggle selectedTogger = group.getSelectedToggle();" However, whenever I put the button.getSelectedToggle in the event handler, it says I can't use it unless I do it as a 'final'. Is there a specific reason why? – stevek Jun 20 '17 at 16:39
  • @stevek You should edit your question to make it clear that's the part you were stuck on. Otherwise the question and answer are of very limited use to other users. – James_D Jun 20 '17 at 16:40
  • @stevek You are welcome. my pleasure to help you. we have to use "final" in Java 7, but it's not necessary in Java 8. My code is under Java 8, so i didn't put "final". here's the answer link what you want to know [Why Java inner classes require “final” outer instance variables](https://stackoverflow.com/questions/3910324/why-java-inner-classes-require-final-outer-instance-variables) – yab Jun 20 '17 at 16:48