w3resource

JavaFX Country selection application

JavaFx User Interface Components: Exercise-9 with Solution

Write a JavaFX application that builds a country selection form using a ChoiceBox or ComboBox. Populate the dropdown with a list of countries, and when a country is selected, display its capital in a label.

Sample Solution:

JavaFx Code:

//Main.java

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.util.HashMap;
import java.util.Map;

public class Main extends Application {

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

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Country Selection App");

        // Create a label to display the selected country's capital.
        Label capitalLabel = new Label("Capital: ");

        // Create a ComboBox with a list of countries.
        ComboBox countryComboBox = new ComboBox<>(FXCollections.observableArrayList(
                "United States", "Canada", "United Kingdom", "Germany", "France", "Japan", "Australia"
        ));

        // Create a map to store the capitals of the countries.
        Map countryCapitalMap = new HashMap<>();
        countryCapitalMap.put("Australia", "Canberra");
		countryCapitalMap.put("Canada", "Ottawa");
		countryCapitalMap.put("France", "Paris");
		countryCapitalMap.put("Germany", "Berlin");
		countryCapitalMap.put("India", "New Delhi");
		countryCapitalMap.put("Japan", "Tokyo");
		countryCapitalMap.put("United Kingdom", "London");
		countryCapitalMap.put("United States", "Washington, D.C.");		

        // Handle the action when a country is selected from the ComboBox.
        countryComboBox.setOnAction(event -> {
            String selectedCountry = countryComboBox.getValue();
            String capital = countryCapitalMap.get(selectedCountry);
            capitalLabel.setText("Capital: " + capital);
        });

        // Create a layout (VBox) to arrange the ComboBox and label.
        VBox root = new VBox(10);
        root.getChildren().addAll(countryComboBox, capitalLabel);

        // Create the scene and set it in the stage.
        Scene scene = new Scene(root, 300, 200);
        primaryStage.setScene(scene);

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

In the above JavaFX application, we use a ComboBox to select a country. A map is used to store the capitals of each country. When a country is selected from the ComboBox, it retrieves the capital from the map and displays it in a label. The elements are organized using a 'VBox'.

Sample Output:

JavaFx: JavaFX Country selection application
JavaFx: JavaFX Country selection application
JavaFx: JavaFX Country selection application

Flowchart:

Flowchart: JavaFX Country selection application.

Java Code Editor:

Previous: JavaFX Month days calculator.
Next: JavaFX CheckBox selection.

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-9.php