How to Simulate a Select All That Apply (SATA) Test Question in JavaFX

Java


In this article, we show how to simulate a select all that apply (SATA) test question in JavaFX.

A SATA question is a question that has multiple options and 1 or more options may be the correct choice. So a SATA question may have 5 answer choices and 3 out of the 5 options are correct.

Unlike multiple choice questions, where only 1 answer option is correct, a SATA question may have multiple correct options. So a user has to choose all of the correct options to get the question correct.

So to simulate the answer options of a SATA question, we will use check boxes. Check boxes are the choice to use because with check boxes, multiple options can be selected.

Radio buttons, on the other hand, are ideal for multiple choice questions, because with multiple choice questions only one answer is correct.

So there are several things that goes into this SATA test question.

When the window first loads, there is a question, the test question.

Then there are 4 check boxes, representing 5 answer options.

Then there is a button, functioning as the submit button.

That's the basic layout. Now let's talk about the functionality.

So when the question first loads, none of the option are chosen. Therefore, we make the submit button disabled. This is because when none of the options are chosen, we don't want the user to be able to submit an answer when the user hasn't chosen any choice.

After the user clicks on an option, the submit button is enabled, meaning the user can now submit an answer.

After the user clicks on the submit button and submits the answer, we then disable the submit button. We don't want the users changing answers after it's submitted.

So this is the layout and functionality of this select all that apply test question.

The full Java code for this SATA question is shown below.





So just like any JavaFX program, we import all the packages we need to run our code.

We create our public class that extends the Application class, so that we inherit all of the code (classes, methods, etc.) in the Application class.

We override the start() method, which is found in the Application class.

We set the title, create the question by making a label, and create another label that will be used to give output to the user on whether the correct answer was chosen or not.

We then create the button and name it button. We put the text "Submit" on the button.

We then create our check boxes.

We then disable the submit button by default initially because when the window first loads, none of the answer options are chosen. Therefore, We don't want the user to be able to submit an answer without any options chosen. We disable the button by the line, button.setDisable(true);

Next we add event handlers to all the check boxes, so that if any of them are clicked and selected, the button will be enabled (or undisabled).

How we do this is through the line, checkbox1.setOnAction(e -> button.setDisable(false) );

And then we repeat it for all the other check boxes.

What this code does is when a check box is clicked, the button gets enabled. It, therefore, now can be clicked and the answer can be submitted for the question.

Now we get to the event handler for the button. When the user clicks on the button, the e parameter is triggered, because the clicks represents an event. After this is done, this is where we want to put our code to disable the button.

So in the code that handles the event handler for the button, we disable the button. This is so that the user can't change answer after they have submitted the answer.

The correct answer to the question, "What of the following are animals?" is Tiger, Lion and Bear, which are check boxes 2, 3, and 5. 1, Therefore, if check boxes 2, 3, and 5 are selected, we output that this is the correct answer. We also then disable the button using the statement, button.setDisable(true);

Since all the other choices are wrong, we have an else statement that states that the answer is wrong. In the same manner, we disable the button using the statement, button.setDisable(true);

This is done so that the user can't answer the question again once submitted.

The rest of the code finishes building the window. We create the layout, add all elements to the layout, create the scene, add the scene to the stage, and show the stage.

We then launch the program through the main method.

Running the code above gives us the following shown at the link: JavaFX SATA Test Question.

And this is all that is necessary to simulate a select all that apply (SATA) test question in JavaFX.


Related Resources

How to Simulate a Multiple Choice Test Question in JavaFX




HTML Comment Box is loading comments...