Ex.
No: Developing an application using JavaFX controls,
Date: layouts and menu
AIM:
To develop an application using JavaFX controls, layouts and menu.
ALGORITHM:
1. Start the Program.
2. Initialize GUI Components : Create input fields, labels, and buttons. Establish a
result label.
3. Create Menu : Set up a menu bar with file and billing options.
4. Design Layout : Use a grid pane for layout with appropriate spacing.
5. Create Bill Method : Implement a method for creating a bill; update result label.
6. Pay Bill Method : Implement a method for paying a bill; update result label.
7. Generate Bill Method: Extract input, calculate total bill, and display an alert.
Update the result label.
8. Main Application Logic: Launch JavaFX, set up main scene with menu and
content. Handle button actions.
9. End the Program.
PROGRAM:
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import javafx.scene.layout.VBox;
import java.time.LocalDate;
public class RailwayReservationSystem extends Application {
private Stage primaryStage;
private Scene reservationScene;
private Scene confirmationScene;
@Override
public void start(Stage primaryStage) {
this.primaryStage = primaryStage;
primaryStage.setTitle("Railway Reservation System");
// Create UI controls for reservation scene
Label nameLabel = new Label("Passenger Name:");
TextField nameTextField = new TextField();
Label destinationLabel = new Label("Destination:");
ChoiceBox<String> destinationChoiceBox = new ChoiceBox<>();
destinationChoiceBox.getItems().addAll("Station A", "Station B", "Station C");
Label dateLabel = new Label("Date:");
DatePicker datePicker = new DatePicker(LocalDate.now());
Label firstClassLabel = new Label("First Class:");
CheckBox firstClassCheckBox = new CheckBox("Yes");
Button reserveButton = new Button("Reserve Ticket");
// Set up layout for reservation scene
GridPane reservationGridPane = new GridPane();
reservationGridPane.add(nameLabel, 0, 0);
reservationGridPane.add(nameTextField, 1, 0);
reservationGridPane.add(destinationLabel, 0, 1);
reservationGridPane.add(destinationChoiceBox, 1, 1);
reservationGridPane.add(dateLabel, 0, 2);
reservationGridPane.add(datePicker, 1, 2);
reservationGridPane.add(firstClassLabel, 0, 3);
reservationGridPane.add(firstClassCheckBox, 1, 3);
reservationGridPane.add(reserveButton, 1, 4);
// Set up event handlers for reservation scene
reserveButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
showConfirmationScene(nameTextField.getText(), destinationChoiceBox.getValue(),
datePicker.getValue(), firstClassCheckBox.isSelected());
}
});
reservationScene = new Scene(reservationGridPane, 400, 250);
// Create UI controls for confirmation scene
Label confirmationLabel = new Label("Reservation Confirmed!");
Button backButton = new Button("Back to Reservation");
// Set up layout for confirmation scene
GridPane confirmationGridPane = new GridPane();
confirmationGridPane.add(confirmationLabel, 0, 0);
confirmationGridPane.add(backButton, 0, 1);
// Set up event handlers for confirmation scene
backButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
showReservationScene();
}
});
confirmationScene = new Scene(confirmationGridPane, 300, 150);
// Set up menu bar
MenuBar menuBar = new MenuBar();
Menu fileMenu = new Menu("File");
MenuItem exitMenuItem = new MenuItem("Exit");
fileMenu.getItems().add(exitMenuItem);
Menu helpMenu = new Menu("Help");
MenuItem aboutMenuItem = new MenuItem("About");
helpMenu.getItems().add(aboutMenuItem);
menuBar.getMenus().addAll(fileMenu, helpMenu);
// Set up event handler for menu items
exitMenuItem.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
primaryStage.close();
}
});
aboutMenuItem.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
showAboutDialog();
}
});
// Set up the main layout
VBox mainLayout = new VBox();
mainLayout.getChildren().addAll(menuBar, reservationGridPane);
// Set up scene
Scene mainScene = new Scene(mainLayout, 400, 300);
// Set up initial scene
primaryStage.setScene(mainScene);
// Show the stage
primaryStage.show();
}
private void showReservationScene() {
primaryStage.setScene(reservationScene);
}
private void showConfirmationScene(String passengerName, String destination, LocalDate date,
boolean isFirstClass) {
System.out.println("Ticket reserved for: " + passengerName);
System.out.println("Destination: " + destination);
System.out.println("Date: " + date);
System.out.println("First Class: " + isFirstClass);
primaryStage.setScene(confirmationScene);
}
private void showAboutDialog() {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("About");
alert.setHeaderText("Railway Reservation System");
alert.setContentText("A simple railway reservation system using JavaFX.");
alert.showAndWait();
}
public static void main(String[] args) {
launch(args);
}
}
OUTPUT:
RESULT:
Thus the application using JavaFX controls, menu and layouts has been developed
successfully.