Returnin index of chosen RadioButton, elements are taken from the array

Viewed 52

I'm working on a javafx quiz, doing something like kahoot

I've got 4 radio buttons, with test option each, which are taken from the array of options

array of options is just an array with 4 options, which are than given to my 4 buttons, but the oder is shuffled so I need to retrieve the index of the button that was clicked

to do so I need setter and getter methods for retrieving the index of chosen RadioButton

Please help me

I have no idea how to return index of chosen element from the button

ToggleGroup toggleGroup = new ToggleGroup();

        rdBtn1.setToggleGroup(toggleGroup);
        rdBtn2.setToggleGroup(toggleGroup);
        rdBtn3.setToggleGroup(toggleGroup);
        rdBtn4.setToggleGroup(toggleGroup);
RadioButton[] radioButtons = {rdBtn1, rdBtn2, rdBtn3, rdBtn4};
 if (getIndex() != -1) {
    int j = getIndex();
    for (int i = 0; i < 4; i++) {
        radioButtons[i].setSelected(i == j);
    }
}
rdBtn.setOnAction(e -> {
    setIndex(0);
    int index = getIndex();
    String userChoice = getOptionAt(index);
  
}); 
setIndex(0); getIndex();

what I really need is these two methods that will get and set index of the button after it has been clicked

1 Answers

An example of retrieving info from togglegroup . in this case the index of selected radiobutton in radiobuttons[]. this is one class javafx app you can test it

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;



public class App extends Application {
    @Override
    public void start(Stage stage) {
       

        
        ToggleGroup toggleGroup = new ToggleGroup();
        RadioButton radioButton = new RadioButton("one");
        radioButton.setToggleGroup(toggleGroup);
        RadioButton radioButton1 = new RadioButton("two");
        radioButton1.setToggleGroup(toggleGroup);
        RadioButton radioButton2 = new RadioButton("three");
        radioButton2.setToggleGroup(toggleGroup);
        
        //array of radioButtons
        RadioButton radioButtons[]={radioButton,radioButton1,radioButton2};
        
        
        String labelText = "the index of selected button is : ";
        Label label = new Label("select a radiobutton");
        HBox hb = new HBox(radioButton,radioButton1,radioButton2,label);
        hb.setAlignment(Pos.CENTER);
        hb.setSpacing(20.0);
        
        toggleGroup.selectedToggleProperty().addListener((e) -> {
            
            RadioButton rb  = (RadioButton)toggleGroup.getSelectedToggle();
            for (int i = 0; i < radioButtons.length; i++) {
                if(rb.equals(radioButtons[i])){
                label.setText(labelText+i);
                break;}
                                     
            }});
            
                
        Scene scene = new Scene(hb,600,400);
        stage.setScene(scene);
        stage.show();
    }

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

radiobutton example

Related