Answer to Question #267288 in Java | JSP | JSF for Ikraam Kader

Question #267288


Q.2.1 On the form create two combo boxes, one for the country selection and another to select a town. Also, add two radio buttons for the user to select to display either a phone or postal code.

Q.2.2 You are also required to create a “submit” and “exit” button. The layout of the form is left to your discretion. Marks will be allocated to the presentation and effectiveness of the layout, but the following layout is displayed for your convenience. 16; 17; 18 2018 ©

Q.2.3 The application must allow a user to select between two countries, namely South Africa and the United Kingdom. When a particular country is selected, present the available towns in the town combo box.

SOUTH AFRICA UNITED KINGDOM Cape Town Phone Code: 021 Postal Code: 8000 London Phone Code: 020 Postal Code: WC2N5DU Johannesburg Phone Code: 011 Postal Code: 2000 Oxford Phone Code: 01865 Postal Code: OX13PN Durban Phone Code: 031 Postal Code: 4000 Southampton Phone Code: 023 Postal Code: SO147DW



1
Expert's answer
2021-11-16T19:31:42-0500
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        String[][] towns = {{"Cape Town", "Johannesburg", "Durban"}, {"London", "Oxford", "Southampton"}};
        String[][] phoneCodes = {{"021", "011", "031"}, {"020", "01865", "023"}};
        String[][] postalCodes = {{"8000", "2000", "4000"}, {"WC2N5DU", "OX13PN", "SO147DW"}};
        ComboBox<String> townsComboBox = new ComboBox<>();
        ComboBox<String> countriesComboBox = new ComboBox<>(FXCollections.observableArrayList("South Africa", "United Kingdom"));
        countriesComboBox.setOnAction((ActionEvent event) -> {
            int selectedIndex = countriesComboBox.getSelectionModel().getSelectedIndex();
            if (selectedIndex != -1) {
                townsComboBox.setItems(FXCollections.observableArrayList(towns[selectedIndex]));
            }
        });
        RadioButton postalCodeRadioButton = new RadioButton("Postal code");
        RadioButton phoneCodeRadioButton = new RadioButton("Phone code");

        ToggleGroup toggleGroup = new ToggleGroup();
        toggleGroup.getToggles().addAll(postalCodeRadioButton, phoneCodeRadioButton);

        TextField outputTextField = new TextField();
        outputTextField.setEditable(false);
        outputTextField.setMaxWidth(100);

        Button submitButton = new Button("Submit");
        submitButton.setOnMouseReleased((MouseEvent me) -> {
            int countryIndex = countriesComboBox.getSelectionModel().getSelectedIndex();
            int townIndex = townsComboBox.getSelectionModel().getSelectedIndex();
            if (me.getButton() == MouseButton.PRIMARY && countryIndex != -1 && townIndex != -1) {
                Toggle selectedToggle = toggleGroup.getSelectedToggle();
                if (selectedToggle != null) {
                    if (selectedToggle.equals(postalCodeRadioButton)) {
                        outputTextField.setText(postalCodes[countryIndex][townIndex]);
                    } else if (selectedToggle.equals(phoneCodeRadioButton)) {
                        outputTextField.setText(phoneCodes[countryIndex][townIndex]);
                    }
                }

            }
        });
        Button exitButton = new Button("Exit");
        exitButton.setOnMouseReleased((MouseEvent me) -> {
            if (me.getButton() == MouseButton.PRIMARY) {
                System.exit(0);
            }
        });

        HBox buttonHBox = new HBox(10, submitButton, exitButton);
        buttonHBox.setAlignment(Pos.CENTER);

        VBox vBox = new VBox(10, countriesComboBox, townsComboBox, postalCodeRadioButton, phoneCodeRadioButton,
                outputTextField, buttonHBox);
        vBox.setAlignment(Pos.CENTER);

        primaryStage.setScene(new Scene(new StackPane(vBox), 800, 600));
        primaryStage.show();
    }

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

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS