# JavaFX

Source: https://wisconsin.twango.dev/sp25-cs400/topics/java-fx

# JavaFX

## Getting Started

Assuming you have JavaFX installed in the parent directory, you can compile and run a JavaFX program using the following commands.

```bash title="Compilation"
javac --module-path ../javafx/lib/ --add-modules javafx.controls HelloWorld.java
```

```bash title="Run JavaFx"
java --module-path ../javafx/lib/ --add-modules javafx.controls HelloWorld
```

## Layouts

JavaFX provides several layout classes that help arrange UI components in a structured manner. Here are some commonly used layouts:

- **VBox**: Arranges components vertically in a single column.
- **HBox**: Arranges components horizontally in a single row.
- **GridPane**: Arranges components in a grid of rows and columns.
- **BorderPane**: Divides the layout into five regions: top, bottom, left, right, and center.
- **FlowPane**: Arranges components in a flow, wrapping to the next line when the space is filled.

## Hierarchy

JavaFX uses a scene graph to represent the UI components. A `Stage` is a window, not a scene-graph node. It holds a `Scene`; the scene has a root `Parent` node containing child nodes. The hierarchy can be visualized as follows:

```
Stage
└── Scene
    └── Node (e.g., VBox, HBox, Button, Label, etc.)
```

## Controls

JavaFX provides a rich set of UI controls that can be used to create interactive applications. Some commonly used controls include:

- **Button**: A clickable button that can trigger actions.
- **Label**: A non-editable text element used to display information.
- **TextField**: An editable text input field.
- **TextArea**: A multi-line editable text input field.
- **CheckBox**: A box that can be checked or unchecked.

## Events

- Direct User Input
    - `MouseEvent`
    - `KeyEvent`
    - `TouchEvent`
- Control Specific
    - `ScrollEvent`
    - `EditEvent`
    - `ActionEvent`
- OS Notifications
    - `WindowEvent`
    - `WebEvent`
    - `ChangeListener`

### Lifecycle

- Event Filters
    - Steps from scene’s root node, down through target node: giving each node a chance to respond to this new event before it reaches the target.
- Event Handlers
    - After all filters have a chance to run, steps from target node back up through root node: giving each node a chance to respond after the target.
- Consumption
    - Events can be consumed within any node’s filter or handler, and this prevents that event from reaching the further steps in its normal lifecycle.

## Connections

- [[sp25-cs400/topics/index|Topic index]]

Obvious technical and formatting errors repaired.
