Previously, how to lay out the interface through fxml file is described. However, static interface can not handle business at all. Code of business logic must be written separately to respond to button click events and present business results even on the interface.Obviously, no Java code can be written inside the fxml, and the entry program has managed the controls to the fxml file, nor can it interfere with the operation of the controls in the Application code.Since the entire interface is entrusted to fxml, the ringer must also ring, and only subsequent logical controllers must be specified by fxml.This is done by adding the attribute "fx:controller" to the root node of the fxml to set the controller path for the current interface.For example, the previous login layout file, login_with_flow.fxml, whose root node is the FlowPane streaming pane, adds the value of "fx:controller" to the FlowPane node, as detailed label examples can be found below:
<FlowPane fx:controller="com.javafx.fxml.LoginController" xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="5" vgap="5">
As you can see from the FlowPane tag above, it specifies a controller path of "com.javafx.fxml.LoginController", which is the controller code corresponding to login_with_flow.fxml.As a partner exclusive to FXML, the controller must also conform to certain formatting specifications. First, it must implement the interface Initializable and override the initialize method defined by the interface, which is called as the name implies when the interface is initialized.Second, the control numbers defined by the FXML file need to be declared inside the controller so that each control object can be manipulated by the control numbers.Here is a code example for a controller template:
//Interface controllers must implement self-interface Initializable public class LoginController implements Initializable { @FXML private Control type control number; // Where the control type is the control class name of JavaFX and the control number is taken from the fx:id in the fxml file @Override public void initialize(URL location, ResourceBundle resources) { // Initialization after interface opening // Here you can set the click event or the selected event for each control, as well as the text font and its size on the control. } }
As an example of the login window described above, its layout file name is login_with_flow.fxml, and the fxml file sets the controller corresponding to the interface called LoginController.Notice that the login window has two radio buttons and one login button, each of which should trigger a click or select event, so in the controller's code several control objects are declared to be operated on, with the object name identical to the fx:id in the fxml.Then override the initialize method of the controller, in which each calls the setOnAction method of the three buttons to register the trigger event after a button is clicked or selected.An example of writing the controller code for the login window based on the above instructions is as follows:
//Interface Controller for Logon Window public class LoginController implements Initializable { @FXML private RadioButton rbPassword; // Radio button for password login @FXML private RadioButton rbVerifycode; // Radio button for Authentication Code Login @FXML private Label labelUser; // User name label @FXML private TextField fieldUser; // User name input box @FXML private Label labelPassword; // Password Label @FXML private PasswordField fieldPassword; // Password Input Box @FXML private Button btnLogin; // Logon button @FXML private Label labelLoginResult; // Logon Result Label @Override public void initialize(URL location, ResourceBundle resources) { // Initialization after interface opening rbPassword.setOnAction(e -> { // Events triggered when the Password Login radio button is selected labelUser.setText("User name:"); labelPassword.setText("dense Code:"); }); rbVerifycode.setOnAction(e -> { // Events triggered when the Authentication Code Logon radio button is selected labelUser.setText("Cell-phone number:"); labelPassword.setText("Verification Code:"); }); // btnLogin.setOnAction(e -> { // Events triggered after clicking the Login button String result = String.format("The user name you entered is%s,Password is%s", fieldUser.getText(), fieldPassword.getText()); labelLoginResult.setText(result); // Display login information on the login results tab }); } }
As you can see from the code above, the logic of this controller is very simple. When a button is selected, only the text label is set with the specified text, which makes it easy to see the result of the control's operation.
Back in the login window entry code LoginMain, run the test program and pop up the login interface as shown in the following figure.
Visible from the diagram, the login window defaults to "password login", and then clicks the "Authentication Code Login" button on the right. The user name label below changes to "mobile phone number:", and the password label changes to "authentication code:", indicating that the selected event of the "Authentication Code Login" button is triggered normally.Then fill in the user name and password boxes in the user name input box and the password input box respectively, and click the login button below. The interface effect of the login window is shown below.
Otherwise, the text label below the login button shows the user name and password information you entered, and you know that the login button click event responded correctly.
For more Java technical articles see " Java Development Notes (Sequence) Chapter Program Directory>