JavaFX Color choice application

JavaFx User Interface Components: Exercise-7 with Solution

Write an JavaFx application that creates a ChoiceBox with a list of colors. When a color is selected, display it in a label or change the background color of a rectangle to the selected color.

Sample Solution:

JavaFx Code:


import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class Main extends Application {

    public static void main(String[] args) {

    public void start(Stage primaryStage) {
        primaryStage.setTitle("Color Choice App");

        // Create a label to display the selected color.
        Label colorLabel = new Label("Selected Color: ");

        // Create a ChoiceBox with a list of colors.
        ChoiceBox colorChoiceBox = new ChoiceBox<>();
        colorChoiceBox.getItems().addAll("Red", "Green", "Blue", "Yellow", "Orange", "Purple");

        // Create a rectangle with an initial color.
        Rectangle colorRectangle = new Rectangle(100, 100, Color.WHITE);

        // Handle the action when a color is selected from the ChoiceBox.
        colorChoiceBox.setOnAction(event -> {
            String selectedColor = colorChoiceBox.getValue();
            Color selectedFillColor = Color.BLACK; // Default color if not found
            switch (selectedColor) {
                case "Red":
                    selectedFillColor = Color.RED;
                case "Green":
                    selectedFillColor = Color.GREEN;
                case "Blue":
                    selectedFillColor = Color.BLUE;
                case "Yellow":
                    selectedFillColor = Color.YELLOW;
                case "Orange":
                    selectedFillColor = Color.ORANGE;
                case "Purple":
                    selectedFillColor = Color.PURPLE;
            colorLabel.setText("Selected Color: " + selectedColor);

        // Create a layout (HBox) to arrange the ChoiceBox, label, and rectangle.
        HBox root = new HBox(10);
        root.getChildren().addAll(colorChoiceBox, colorLabel);

        // Create a StackPane to contain the rectangle.
        StackPane rectangleContainer = new StackPane();

        // Create the scene and set it in the stage.
        Scene scene = new Scene(new HBox(root, rectangleContainer), 400, 200);

        // Set the title of the window.

In the above JavaFX exercise, we have a ChoiceBox containing a list of colors. When a color is selected from the ChoiceBox, it updates both a label with the selected color's name and the background color of a rectangle. The colors and their associated 'Color' objects are handled in a 'switch' statement. The elements are organized using an 'HBox', and the rectangle is placed in a 'StackPane'.

Sample Output:

JavaFx: JavaFX Color choice application
JavaFx: JavaFX Color choice application
JavaFx: JavaFX Color choice application


Flowchart: JavaFX Color choice application.

Java Code Editor:

Previous: JavaFX Password match checker.
Next: JavaFX Month days calculator.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

Follow us on Facebook and Twitter for latest update.