JavaFX: Initialize RadioButton selection status

Viewed 1531

I need to save the selection status of multiple RadioButtons, so I can see which RadioButton is selected, when I go back to the scene later on. It's not about the userData, it's about to see whether it's selected. Right now I know how to make it work but with a lot of messy copy & paste. Something like this for every ToggleGroup:

@FXML private RadioButton rb1;
@FXML private RadioButton rb2;

public static int[] status = new int[600];

// to save it
if (rb1.getSelect(true)){
 status[0] = 1;
} else {
 status[0] = 0;
} 

// to load it
if (status[0] == 1){
 rb1.setSelected(true);
} else {
 rb2.setSelected(true);
} 

The problem is that I program a survey with more than 300 questions with binary answers. So I have more than 600 different RadioButtons. It'd take hours to implement it this way.

Is there any smart way to do it? I'm grateful for any advice. Thanks in advance!

1 Answers

Here is a SCVExample, that contains the simplest implementation based on my comment: It defines a model (Survey and Question) then binds the GUI to this model.

public class Radios extends Application {

    class Survey {
        private ObservableList<Question> questions = FXCollections.observableArrayList();

        public ObservableList<Question> getQuestions() {
            return questions;
        }
    }

    class Question {

        private StringProperty text = new SimpleStringProperty("");
        private BooleanProperty answer = new SimpleBooleanProperty(false);


        public Question(String text) {
            setText(text);
        }


        public boolean isAnswer() {
            return answer.get();
        }

        public BooleanProperty answerProperty() {
            return answer;
        }

        public void setAnswer(boolean answer) {
            this.answer.set(answer);
        }


        public String getText() {
            return text.get();
        }

        public StringProperty textProperty() {
            return text;
        }

        public void setText(String text) {
            this.text.set(text);
        }
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        // Model
        Survey survey = new Survey();
        for (int i = 0; i<300; i++) {
            Question question = new Question("Do you like number " + i + "?");
            question.answerProperty().addListener((obs, oldval,newval) -> {
                System.out.println("Question: " + question.getText() + " answer changed from " + oldval + " to " + newval);
            });
            survey.getQuestions().add(question);
        }

        // View
        VBox root = new VBox();
        root.setSpacing(10);

        for (Question question : survey.getQuestions()) {

            VBox vBox = new VBox();
            vBox.setSpacing(5);
            HBox answerHBox = new HBox();
            answerHBox.setSpacing(20);
            vBox.getChildren().addAll(new Label(question.getText()), answerHBox);

            RadioButton yes = new RadioButton("Yes");
            RadioButton no = new RadioButton("No");
            ToggleGroup toggleGroup = new ToggleGroup();

            yes.setToggleGroup(toggleGroup);
            no.setToggleGroup(toggleGroup);

            answerHBox.getChildren().addAll(yes, no);
            yes.setSelected(question.isAnswer());
            no.setSelected(!question.isAnswer());

            toggleGroup.selectedToggleProperty().addListener((observable, oldValue, newValue) -> {
                question.setAnswer(newValue.equals(yes));
            });


            root.getChildren().add(vBox);

        }

        Scene scene = new Scene(new ScrollPane(root), 500, 500);

        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

This will generate a survey like:

enter image description here

Console output:

Question: Do you like number 1? answer changed from false to true
Question: Do you like number 3? answer changed from false to true
Question: Do you like number 6? answer changed from false to true
Question: Do you like number 8? answer changed from false to true
Question: Do you like number 8? answer changed from true to false
Question: Do you like number 8? answer changed from false to true
Related