The View
Next, create the basic FXML file src/main/resources/ui/main.fxml
:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.control.Button?>
<BorderPane xmlns:fx="http://javafx.com/fxml/1"
xmlns="http://javafx.com/javafx/8"
fx:controller="hello.Controller">
<center>
<Button text="Hello" onAction="#helloClicked"/>
</center>
</BorderPane>
What's happening here
- We add the standard XML header specifying version and encoding.
- Add two imports for the
BorderPane
and theButton
element. - Add a
BorderPane
as the root element for the view.- The
BorderPane
receives thehello.Controller
class as its controller attribute. A view may have only one controller and it will always be defined on the root element. If you want to use multiple controller classes you have to extract a sub-view from your view in a separate file. - A
BorderPane
divides its contents on five zones. We add the<center>
zone: In this zone we add aButton
with a greeting text and an action handler: By starting the value for theonAction
handler with a '#' we state that we specify a member of the controller class. In this case the functionhelloClicked()
of thehello.Controller
class.
- The
Continue with the controller.