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
BorderPaneand theButtonelement. - Add a
BorderPaneas the root element for the view.- The
BorderPanereceives thehello.Controllerclass 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
BorderPanedivides its contents on five zones. We add the<center>zone: In this zone we add aButtonwith a greeting text and an action handler: By starting the value for theonActionhandler with a '#' we state that we specify a member of the controller class. In this case the functionhelloClicked()of thehello.Controllerclass.
- The
Continue with the controller.