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.
You are also required to create a menu system which will allow the user to exit the application under the file menu. Under the tools menu allow the form submission and option to display the average yearly petrol price report. 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.
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.
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.layout.BorderPane;
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", "Polokwane"}, {"London", "Liverpool", "Manchester"}};
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");
postalCodeRadioButton.setOnAction((ActionEvent event) -> {
System.out.println("Selected postal");
});
RadioButton phoneCodeRadioButton = new RadioButton("Phone code");
phoneCodeRadioButton.setOnAction((ActionEvent event) -> {
System.out.println("Selected phone");
});
ToggleGroup toggleGroup = new ToggleGroup();
toggleGroup.getToggles().addAll(postalCodeRadioButton, phoneCodeRadioButton);
MenuItem exitMenuItem = new MenuItem("Exit");
exitMenuItem.setOnAction((ActionEvent event) -> {
System.exit(0);
});
MenuItem submissionMenuItem = new MenuItem("Submission");
submissionMenuItem.setOnAction((ActionEvent event) -> {
});
MenuItem priceReportMenuItem = new MenuItem("Price Report");
priceReportMenuItem.setOnAction((ActionEvent event) -> {
});
Menu fileMenu = new Menu("File");
fileMenu.getItems().add(exitMenuItem);
Menu toolsMenu = new Menu("Tools");
toolsMenu.getItems().addAll(submissionMenuItem, priceReportMenuItem);
MenuBar menuBar = new MenuBar(fileMenu, toolsMenu);
VBox comboBoxesVBox = new VBox(10, countriesComboBox, townsComboBox, postalCodeRadioButton, phoneCodeRadioButton);
comboBoxesVBox.setAlignment(Pos.CENTER);
BorderPane borderPane = new BorderPane();
borderPane.setTop(menuBar);
borderPane.setCenter(comboBoxesVBox);
primaryStage.setScene(new Scene(borderPane, 800, 600));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Comments
Leave a comment