CSC419 ยท 2079 ยท 5 marks
What is JavaFX? Compare it with swing. Explain FlowPane layout of JavaFX.
What is JavaFX? Compare it with swing. Explain FlowPane layout of JavaFX. [5]
Quick answer
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.
JavaFX: Definition, Comparison with Swing, and FlowPane Layout
1. What is JavaFX?
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.
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.