4 Javafx Programming

Advanced Java Programming ยท Unit 4

JavaFX Programming

Exam-focused notes for JavaFX Programming (Advanced Java Programming, BIT401): what the TU syllabus asks and how it has actually been tested, with 4 solved past questions from this unit.

JavaFX Programming in short

JavaFX is the modern Java toolkit for desktop windows. It replaces Swing in new projects and uses a scene graph to arrange the screen.

JavaFX Programming: notes

Steps of writing JavaFX programs

A JavaFX program follows a fixed pattern.

  • Extend the javafx.application.Application class.
  • Override the start(Stage primaryStage) method.
  • Create controls such as Label, Button and TextField.
  • Place them in a layout pane, which becomes the root node.
  • Create a Scene that holds the root.
  • Set the scene on the stage with setScene() and call show().
  • Call launch(args) from main().
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class HelloFX extends Application {
    @Override
    public void start(Stage stage) {
        Button b = new Button("Say hello");
        b.setOnAction(e -> System.out.println("Hello JavaFX"));
        StackPane root = new StackPane(b);
        stage.setTitle("Hello");
        stage.setScene(new Scene(root, 300, 200));
        stage.show();
    }
    public static void main(String[] args) { launch(args); }
}

Scene and stage management

The Stage is the top-level window. The Scene is the content shown in that window. The scene holds a tree of nodes called the scene graph. The root node is a layout pane, and it contains child nodes. One stage shows one scene at a time, but we can swap the scene with setScene(). We can also create more stages for extra windows.

What this unit covers

  • Steps of writing JavaFX programs
  • JavaFX layouts: VBox, HBox, GridPane
  • Login form design
  • Event handling in JavaFX
  • Scene and stage management

JavaFX layouts

2082 ยท 5 marks

Describe any two JavaFX layouts. [5]

Show solved answer

Model Answer: Two JavaFX Layouts

1. BorderPane Layout

BorderPane is a layout container that arranges child nodes in five regions: top, bottom, left, right, and center.

  • Structure: The center region occupies the maximum available space, while the other four regions size themselves based on their content
  • Use case: Ideal for creating application layouts with a menu bar at the top, toolbars on sides, and main content in the center
  • Properties: Each region can hold a single node; if multiple nodes are needed, wrap them in another container (e.g., VBox, HBox)
  • Example structure:
    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
    โ”‚       TOP           โ”‚
    โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”ค
    โ”‚      โ”‚       โ”‚      โ”‚
    โ”‚ LEFT โ”‚CENTER โ”‚RIGHT โ”‚
    โ”‚      โ”‚       โ”‚      โ”‚
    โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”ค
    โ”‚      BOTTOM         โ”‚
    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
    

2. GridPane Layout

GridPane is a layout container that arranges child nodes in a flexible grid of rows and columns.

  • Structure: Nodes are positioned using row and column indices; cells can span multiple rows/columns
  • Flexibility: Rows and columns automatically adjust their size based on content; can set preferred widths/heights
  • Properties: Supports gap spacing between rows and columns, alignment, and padding
  • Use case: Suitable for form layouts, data tables, and any grid-based arrangement of controls
  • Example: A login form with labels in column 0 and text fields in column 1, arranged across multiple rows

Key Difference: BorderPane provides a fixed five-region layout, while GridPane offers flexible row-column positioning for more complex arrangements.

Open this answer on its own page โ†’

2081 ยท 5 marks

Describe about any three types of JavaFX layouts. [5]

JavaFX provides several layout panes to organize and position UI components. Here are three important types: --- BorderPane divides the layout area into five regions: top, bottom, left, right, and center. - Structure: The center region expands to fill available space; edge regions take only requi...

Open this answer on its own page โ†’

Steps of writing JavaFX programs

2080 ยท 5 marks

Discuss the steps of writing JavaFX programs. [5]

Since reference notes are not provided, I'll outline the standard steps for writing JavaFX programs based on the BSc CSIT curriculum: Import the necessary JavaFX libraries: Create a class that extends javafx.application.Application: Implement the start(Stage primaryStage) method, which is the ent...

Open this answer on its own page โ†’

Login form design

0 ยท 5 marks

Write down a JavaFx program to create login form. [5]

Since reference notes are not provided, this answer follows standard JavaFX best practices aligned with typical BSc CSIT curriculum requirements. Component Purpose -------------------- VBox Container for vertical layout TextField Input field for username PasswordField Secure input field for passw...

Open this answer on its own page โ†’