Introduction
Introduction
@Override
public void start(Stage primaryStage) {
// Create a container as root node in the Scene
FlowPane root = new FlowPane();
// Set appearance of container (spacing, alignment)
Looks ugly.
Run-time Annoyance
When you run a JavaFX application with Java 11 you
may get this message:
Cmd line:
java --module-path /path/to/javafx/lib
--add-modules javafx.base,javafx.controls
IDE: Add --module-path and --add-modules to VM args.
Java 8 - Retrograde Solution
Java 8 includes JavaFX in the JDK (no external Jars)
and does not use modules.
Event Handler
button.setOnAction( EventHandler<ActionEvent> )
== or ==
class ClickHandler
implements EventHandler<ActionEvent> {
public void handle(ActionEvent event) {
String name = nameField.getText().trim();
if (name.isEmpty()) {
nameField.setPromptText(
"Please enter a name");
}
else showDialog("Hello, "+name);
}
}
Access the TextField
EventHandler needs access to the nameField.
Define it as an attribute instead of a local variable.
button.setOnAction(new ClickHandler());
showDialog
Instead of printing on boring System.out,
pop-up an Alert box to greet user.
TODO 1:
TODO 2:
After greeting the
If user presses ENTER in
person, clear the text
nameField, also invoke
from nameField.
ClickHandler, by adding an event
handler to nameField.
You can reuse the same
ClickHandler object, don't create
another one.
SceneBuilder
Visual tool for creating graphical UI. But first...
Writing a UI in Code is Good
Good to learn the concepts and components
first.