Java Reference
In-Depth Information
title.bind(titleTextField.textProperty());
stage.titleProperty().bind(title);
stage.initStyle(stageStyle);
}
@FXML
public void toBackEventHandler(ActionEvent e) {
stage.toBack();
}
@FXML
public void toFrontEventHandler(ActionEvent e) {
stage.toFront();
}
@FXML
public void closeEventHandler(ActionEvent e) {
stage.close();
}
@FXML
public void mousePressedHandler(MouseEvent me) {
dragAnchorX = me.getScreenX() - stage.getX();
dragAnchorY = me.getScreenY() - stage.getY();
}
@FXML
public void mouseDraggedHandler(MouseEvent me) {
stage.setX(me.getScreenX() - dragAnchorX);
stage.setY(me.getScreenY() - dragAnchorY);
}
}
This class is extracted from the
StageCoachMain
class of Chapter 2, and this is the class that we specified as the
controller class for the FXML file
StageCoach.fxml.
Indeed, it includes fields whose types and names match the
fx:id
s in the FXML file. It also includes methods whose names and signatures match those specified as the event
handlers for the various nodes in the FXML file.
The only thing that needs some explanation is the
@FXML
annotation. It belongs to the
javafx.fxml
package. This
is a marker annotation with a runtime retention that can be applied to fields and methods. When applied to a field,
the
@FXML
annotation tells JavaFX SceneBuilder the field's name can be used as the
fx:id
of the appropriately typed
elements in an FXML file. When applied to a method, the
@FXML
annotation tells JavaFX SceneBuilder the method's
name can be used as the value of the appropriately typed event handler attributes. Fields and methods annotated with
@FXML
are made accessible to the FXML loading facility regardless of the modifiers. Therefore it is safe to change all the
@FXML
annotated fields from
public
to
private
without adversely affecting the FXML loading process.
The
StageCoachController
class includes matching fields for all the
fx:id
s declared in the FXML file. It also
includes the five event handler methods to which the event handler attributes in the FXML file point. All of these fields
and methods are annotated with
@FXML
.
The
StageCoachController
also includes some fields and methods that are not annotated with the
@FXML
annotation. These fields and methods are present in the class for other purposes. For example, the
stage
field, the
setStage()
, and the
setupBindings()
methods are used directly in Java code.
Search WWH ::
Custom Search