Thompson Rivers University
Question 1
Lab 5 JavaFX - SM 2025Explain what would happen if the ToggleGroup had not been set on the radio buttons used in the QuoteOptions program.
These are the classes from that example:
QuoteOptions.java
public class QuoteOptions extends Application {
public void start(Stage primaryStage) {
QuoteOptionsPane pane = new QuoteOptionsPane();
pane.setAlignment(Pos.CENTER);
pane.setStyle("-fx-background-color: lightgreen");
Scene scene = new Scene(pane, 500, 150);
primaryStage.setTitle("Quote Options");
primaryStage.setScene(scene);
primaryStage.show();
}
}QuoteOptionsPane.java
public class QuoteOptionsPane extends HBox {
private Text quote;
private String philosophyQuote, carpentryQuote, comedyQuote;
private RadioButton philosophyButton, carpentryButton, comedyButton;
public QuoteOptionsPane() {
// Quote strings
philosophyQuote = "I think, therefore I am.";
carpentryQuote = "Measure twice. Cut once.";
comedyQuote = "Take my wife, please.";
// Text element
quote = new Text(philosophyQuote);
quote.setFont(new Font("Helvetica", 24));
StackPane quotePane = new StackPane(quote);
quotePane.setPrefSize(300, 100);
// Toggle group for radio buttons
ToggleGroup group = new ToggleGroup();
philosophyButton = new RadioButton("Philosophy");
philosophyButton.setSelected(true);
philosophyButton.setToggleGroup(group);
philosophyButton.setOnAction(this::processRadioButtonAction);
carpentryButton = new RadioButton("Carpentry");
carpentryButton.setToggleGroup(group);
carpentryButton.setOnAction(this::processRadioButtonAction);
comedyButton = new RadioButton("Comedy");
comedyButton.setToggleGroup(group);
comedyButton.setOnAction(this::processRadioButtonAction);
// Layout the radio buttons vertically
VBox options = new VBox(philosophyButton, carpentryButton, comedyButton);
options.setAlignment(Pos.CENTER_LEFT);
options.setSpacing(10);
setSpacing(20);
getChildren().addAll(options, quotePane);
}
public void processRadioButtonAction(ActionEvent event) {
if (philosophyButton.isSelected()) {
quote.setText(philosophyQuote);
} else if (carpentryButton.isSelected()) {
quote.setText(carpentryQuote);
} else {
quote.setText(comedyQuote);
}
}
}Members only
Keep reading this lesson
You've reached the end of the free preview. Unlock the rest of this lesson plus 54 more lessons in Thompson Rivers University.
- Unlimited coding questions
- Unlimited access to all classroom material
- Weekly live call for questions and guidance
- 3,000 AI tutor tokens / month
- Personal SQL database (3 MB)
- Progress tracking
Cancel any time.