CJCoding With Joseph
15free / daySign inCreate account

Thompson Rivers University

Adding Color Change Logic

Assignment 5 User Interfaces FX - SM 2025

Right now, our three radio buttons appear in the app and let the user choose between Red, Green, and Orange, but nothing happens when you select them.

In this step, we’ll make it so that when the user clicks one of the radio buttons, the square instantly changes to that color.

We’ll do this by adding a listener to the radio button group. A listener is a little piece of code that runs whenever something changes. So in this case, when a different radio button is selected.


Set Up the Listener

The colorGroup already holds all three radio buttons. We can attach a listener to it using selectedToggleProperty().addListener(...).

Let’s break down what this is doing:

  • colorGroup.selectedToggleProperty() watches the selected radio button.

  • When it changes, our listener runs.

  • We typecast newToggle back to a RadioButton so we can check which one it is.

  • We use getText() to get the label on that button (like "Red").

  • Then we use a switch statement to change the square’s color to match using setFill(...)

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.