0% found this document useful (0 votes)
3 views3 pages

Question 5

The document provides a JavaFX application code for a simple library system. It includes a GUI with fields for entering book details such as title, author, and ISBN, and a button to submit the information. Upon submission, a new Book object is created and its details are printed to the console.

Uploaded by

msego237
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Question 5

The document provides a JavaFX application code for a simple library system. It includes a GUI with fields for entering book details such as title, author, and ISBN, and a button to submit the information. Upon submission, a new Book object is created and its details are printed to the console.

Uploaded by

msego237
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Question 5: GUI Development with JavaFX (20 Marks)

package com.example.demo4;

import javafx.application.Application; // Importing the Application class to create a


JavaFX application
import javafx.scene.Scene; // Importing Scene class for the GUI scene
import javafx.scene.control.*; // Importing control classes for UI components like
TextField and Button
import javafx.scene.layout.*; // Importing layout classes for organizing UI
components
import javafx.stage.Stage; // Importing Stage class to create the main window

// Main class extending Application to create a JavaFX application


public class LibraryApp extends Application {

// The start method is the entry point for JavaFX applications


@Override
public void start(Stage primaryStage) {
// Creating a VBox layout to arrange components vertically
VBox layout = new VBox(10); // 10 pixels of spacing between elements

// Creating labels and text fields for title, author, and ISBN
Label titleLabel = new Label("Title:"); // Label for the title input
TextField titleField = new TextField(); // TextField for user to input the title

Label authorLabel = new Label("Author:"); // Label for the author input


TextField authorField = new TextField(); // TextField for user to input the author

Label isbnLabel = new Label("ISBN:"); // Label for the ISBN input


TextField isbnField = new TextField(); // TextField for user to input the ISBN

// Creating a button for submitting the book details


Button submitButton = new Button("Add Book"); // Button labeled "Add Book"

// Event handler for the submit button


submitButton.setOnAction(event -> {
// Collecting input values from text fields
String title = titleField.getText(); // Getting the title from the text field
String author = authorField.getText(); // Getting the author from the text field
String isbn = isbnField.getText(); // Getting the ISBN from the text field

// Creating a Book object with the input values


Book newBook = new Book(title, isbn); // Assuming Book class has a
constructor with title and ISBN
// Printing the book's details to the console
System.out.println("Book Added: " + newBook); // Outputting the Book object
to the console
});

// Adding all components to the layout


layout.getChildren().addAll(titleLabel, titleField, authorLabel, authorField,
isbnLabel, isbnField, submitButton);//adding authorlabels, field, authorfield,isbnfield

// Creating a Scene with the layout and setting dimensions


Scene scene = new Scene(layout, 300, 200); // Width: 300px, Height: 200px

// Setting the title of the primary stage (window)


primaryStage.setTitle("Library System"); // Title of the window

// Adding the scene to the stage


primaryStage.setScene(scene); // Setting the scene for the stage

// Displaying the stage


primaryStage.show(); // Making the window visible
}

// Main method to launch the application


public static void main(String[] args) {
launch(args); // Launching the JavaFX application
}
}

// Book class for creating a book object


class Book {//creating book class
private String title; // Title of the book
private String isbn; // ISBN of the book

// Constructor for the Book class


public Book(String title, String isbn) {
this.title = title; // Initializing title
this.isbn = isbn; // Initializing ISBN
}

// toString method to represent the Book object as a string


@Override //overides the method from superclass using tostring method
public String toString() {
return "Book{" + "title='" + title + '\'' + ", isbn='" + isbn + '\'' + '}';
}
}
Output

You might also like