w3resource

JavaFX wireless mode selection application

JavaFx User Interface Components: Exercise-15 with Solution

Write a JavaFX application for choosing a wireless mode using ToggleButtons (e.g., "Bluetooth," "WiFi," "hot-spot"). Highlight the selected wireless mode.

Sample Solution:

JavaFx Code:

Main.java

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class Main extends Application {

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

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Wireless mode Selection");

        // Create a ToggleGroup for the ToggleButtons.
        ToggleGroup toggleGroup = new ToggleGroup();

        // Create ToggleButtons for Wireless mode Selection.
        ToggleButton bluetoothButton = createToggleButton("Bluetooth", toggleGroup);
        ToggleButton wifiButton = createToggleButton("WiFi", toggleGroup);
        ToggleButton hotspotButton = createToggleButton("Hotspot", toggleGroup);

        // Create a layout (HBox) to arrange the ToggleButtons.
        HBox root = new HBox(10);
        root.getChildren().addAll(bluetoothButton, wifiButton, hotspotButton);

        // Create the scene and set it in the stage.
        Scene scene = new Scene(root, 300, 100);
        scene.getStylesheets().add(getClass().getResource("styles.css").toExternalForm()); // Load CSS file
        primaryStage.setScene(scene);

        // Set the title of the window.
        primaryStage.show();
    }

    // Create a ToggleButton and add it to the ToggleGroup.
    private ToggleButton createToggleButton(String text, ToggleGroup toggleGroup) {
        ToggleButton button = new ToggleButton(text);
        button.setToggleGroup(toggleGroup);
        return button;
    }
} 

styles.css

.toggle-button:selected {
    -fx-background-color: lightblue;
}

In the above JavaFX application, we use ToggleButtons to choose between different difficulty levels (Bluetooth, WiFi, Hotspot). When a ToggleButton is selected, it is highlighted with a light blue background. The elements are organized using an 'HBox'.

Note: Place the "styles.css" file in the same directory as your Java source code. This stylesheet defines the background color for selected 'ToggleButtons'.

Sample Output:

JavaFx: JavaFX wireless mode selection application.
JavaFx: JavaFX wireless mode selection application.
JavaFx: JavaFX wireless mode selection application.

Flowchart:

Flowchart: JavaFX wireless mode selection application.
Flowchart: JavaFX wireless mode selection application.

Java Code Editor:

Previous: JavaFX ToggleButtons color preference form.
Next: JavaFX Circle radius control application.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/java-exercises/javafx/javafx-user-interface-components-exercise-15.php