6 Gui With Javafx

Advanced Java Programming · Unit 6 · 3 hrs

GUI with JavaFX

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

GUI with JavaFX in short

This unit introduces JavaFX as a modern alternative to Swing for building desktop GUIs, covering its layout panes, its UI controls, and the steps used to assemble a JavaFX application.

GUI with JavaFX: notes

Introduction, JavaFX vs Swing, JavaFX Layouts: FlowPane, BorderPane, Hbox, VBox, GridPane

JavaFX is a Java library for building desktop applications and Rich Internet Applications (RIAs) that run across Windows, Linux, macOS, web, and mobile. It was intended to eventually replace Swing as Java's GUI framework, offering richer built-in support for styling and layout.

SwingJavaFX
The long-standing standard toolkit for Java GUIsDesigned as Swing's eventual successor for desktop applications
Larger, more mature set of ready-made GUI componentsSmaller but growing set of components
No built-in support for styling with CSS or layout with XML (FXML)Supports styling with CSS and declarative layout with FXML
Difficult to build convincing 3D interfacesHas built-in support for 3D graphics and effects
A standalone component librarySeveral of its components are built on top of, and interoperate with, Swing

JavaFX provides several built-in layout panes, each in the javafx.scene.layout package:

  • FlowPane lays its children out in a line that wraps once it runs out of space along the pane's orientation, horizontally by row or vertically by column, similar to how text wraps in a paragraph. Its constructors include FlowPane(), FlowPane(double hgap, double vgap), and FlowPane(Orientation orientation), and it exposes properties such as hgap, vgap, and columnHalignment.
  • BorderPane divides its area into five regions, top, bottom, left, right, and center, set through setTop(), setBottom(), setLeft(), setRight(), and setCenter().
  • HBox arranges its children in a single horizontal row; its spacing property sets the gap between children, and its constructors include HBox() and HBox(double spacing).
  • VBox arranges its children in a single vertical column, mirroring HBox, with the same kind of spacing property and constructors.
  • GridPane arranges children into a flexible grid of rows and columns, where a child can be placed into any cell (and can span more than one, similar in spirit to Swing's GridBagLayout); it has a single no-argument constructor, GridPane(), and exposes properties such as hgap, vgap, and gridLinesVisible.

What this unit covers

  • Introduction, JavaFX vs Swing, JavaFX Layouts: FlowPane, BorderPane, Hbox, VBox, GridPane
  • JavaFX UI Controls: Label, TextField, Button, RadioButton, CheckBox, Hyperlink, Menu, Tooltip, FileChooser

JavaFX UI Controls

2081 · 5 marks

How does JavaFX hyperlink control format text that functions as button? Illustrate with an example. [5]

A Hyperlink in JavaFX is a control that formats text to look and behave like an HTML hyperlink (clickable underlined text), but it also functions as a button -- meaning it generates an ActionEvent when clicked, just like a regular button. - It is represented by the class javafx.scene.control.Hype...

Open this answer on its own page →

2080 · 5 marks

Write a JavaFX application with components, buttons, text fields, and labels, arranged in a VBox or HBox layout. [5]

JavaFX is a modern Java GUI framework. It uses a Scene Graph model where UI elements (nodes) are arranged inside layout panes. Key classes used here: Class Purpose ------ Label Displays static text TextField Accepts user text input Button Triggers an action VBox Arranges nodes vertically HBox Arr...

Open this answer on its own page →

Introduction, JavaFX vs Swing, JavaFX Layouts

2079 · 5 marks

What is JavaFX? Compare it with swing. Explain FlowPane layout of JavaFX. [5]

Show solved answer

JavaFX: Definition, Comparison with Swing, and FlowPane Layout


1. What is JavaFX?

JavaFX is a modern Java platform that provides support for creating rich desktop applications. It offers a wide range of features including support for 2D and 3D graphics, CSS-based styling, and XML-based UI design (using FXML). JavaFX was introduced as a successor to Swing for building graphical user interfaces in Java.


2. Comparison: JavaFX vs Swing

FeatureSwingJavaFX
PurposeStandard toolkit for Java developers for creating GUIProvides platform support for creating modern desktop applications
GUI ComponentsHas a more sophisticated and larger set of GUI componentsHas decent but comparatively fewer GUI components
CSS / XML SupportDoes NOT support customization using CSS and XMLSupports customization using CSS and XML
3D ApplicationsVery difficult to create beautiful 3D applicationsCan easily create beautiful 3D applications
RelationshipActs as a legacy UI component libraryHas several components built over Swing

3. FlowPane Layout in JavaFX

JavaFX provides the following built-in layout panes:

  • FlowPane, HBox, VBox, BorderPane, GridPane

Definition

FlowPane is a layout pane in JavaFX that organizes nodes in a flow that wraps at the FlowPane's boundary.

Types of FlowPane

TypeBehavior
Horizontal FlowPaneArranges nodes in a row and wraps them according to the FlowPane's width
Vertical FlowPaneArranges nodes in a column and wraps them according to the FlowPane's height

Key Points

  • Nodes are placed one after another (horizontally or vertically).
  • When the available space runs out, nodes wrap to the next row or column automatically.
  • This is similar in concept to Java AWT's FlowLayout, but designed specifically for JavaFX.

Example Code

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;

public class FlowPaneExample extends Application {
    @Override
    public void start(Stage primaryStage) {
        FlowPane flowPane = new FlowPane();

        // Adding buttons to FlowPane
        flowPane.getChildren().add(new Button("Button 1"));
        flowPane.getChildren().add(new Button("Button 2"));
        flowPane.getChildren().add(new Button("Button 3"));

        Scene scene = new Scene(flowPane, 300, 200);
        primaryStage.setTitle("FlowPane Example");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

How it Works

  • Buttons are added to the FlowPane one after another.
  • When the width of the pane is exceeded, the remaining buttons wrap to the next line automatically.
  • This makes FlowPane very useful for dynamic, resizable UIs.

Summary: JavaFX is a modern GUI framework for Java that improves upon Swing with CSS/XML support and 3D capabilities. FlowPane is one of its core layout managers that arranges nodes in a flowing, wrapping manner either horizontally or vertically.

Open this answer on its own page →

2078 · 5 marks

Compare JavaFX with swing. Explain HBox and BBox layouts of JavaFX. [5]

Swing JavaFX ----------------------- Swing is the standard toolkit for Java developers in creating GUI. JavaFX provides platform support for creating desktop applications. Swing has a more sophisticated set of GUI components. JavaFX has decent but lesser number of GUI components. Swing does not h...

Open this answer on its own page →