Develop a Java GUI application that will produce an Area lookup application that will allow a user to view a phone or postal code for a Town in a particular Country. Do not forget to use the marking guideline at the end of this question to see how the marks are allocated. 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.
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);
}
}
Comments
Leave a comment