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.
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]
2080 · 5 marks
Write a JavaFX application with components, buttons, text fields, and labels, arranged in a VBox or HBox layout. [5]
Introduction, JavaFX vs Swing, JavaFX Layouts
2079 · 5 marks
What is JavaFX? Compare it with swing. Explain FlowPane layout of JavaFX. [5]
Show solved answerHide 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
| Feature | Swing | JavaFX |
|---|---|---|
| Purpose | Standard toolkit for Java developers for creating GUI | Provides platform support for creating modern desktop applications |
| GUI Components | Has a more sophisticated and larger set of GUI components | Has decent but comparatively fewer GUI components |
| CSS / XML Support | Does NOT support customization using CSS and XML | Supports customization using CSS and XML |
| 3D Applications | Very difficult to create beautiful 3D applications | Can easily create beautiful 3D applications |
| Relationship | Acts as a legacy UI component library | Has 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
| Type | Behavior |
|---|---|
| Horizontal FlowPane | Arranges nodes in a row and wraps them according to the FlowPane's width |
| Vertical FlowPane | Arranges 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
FlowPaneone 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.
2078 · 5 marks
Compare JavaFX with swing. Explain HBox and BBox layouts of JavaFX. [5]
Make Unit 6 stick
Practice CSC419 with flashcards & quizzes