Java Reference
In-Depth Information
piece.ownerProperty().bind(model.board[i][j]);
board.add(new StackPane(square, piece), i, j);
}
}
return board;
}
}
The BoardController class contains a number of variables that are annotated with the @FXML annotation.
As explained in Chapter 3, this technique binds the components defined in the FXML file with JavaFX objects.
When the FXML file is loaded, the initialize() method of the controller will be called. In this method, we create
the required bindings and we populate the gridpane with the tiles.
The required bindings are exactly the same as the bindings we created in the previous sections; for example,
we bind the preferred width for the tiles in the top part of the borderpane as follows:
titlePane.prefTileWidthProperty().bind(Bindings.selectDouble(titlePane.parentProperty(), "width").
divide(2));
Creating the tiles is done in exactly the same way as we did it without FXML. This time, we add the tiles to the
centerPane variable, which corresponds, due to the @FXML annotation, to the element with id centerPane in the FXML
file. Indeed, the FXML file contains the declaration
<StackPane fx:id="centerPane">
and our code contains this statement:
@FXML private StackPane centerPane;
...
centerPane.getChildren().add(tiles());
The combination of these two statements will cause the tiles to be displayed in the StackPane declared in the
FXML file.
The code in Listing 5-19 also shows that it is possible to bind actions on FXML-declared components to method
calls in the controller class. Clicking the restart button will cause the restart() method to be called. Again, this is
achieved by the collaboration between the FXML file and the controller class. The FXML file declares the restart
button and its action as follows:
<Button layoutX="520.0" layoutY="14.0" mnemonicParsing="false" onAction="#restart" text="Restart"
AnchorPane.rightAnchor="10.0" AnchorPane.topAnchor="10.0" />
The onAction attribute that links to the method in the controller class is shown in bold.
In the BoardController class, the restart() function is annotated with @FXML , which provides the binding with
the restart action defined in the FXML file:
@FXML
public void restart() {
model.restart();
}
 
Search WWH ::




Custom Search