Java Reference
In-Depth Information
currentImageView.setPreserveRatio(true);
// resize based on the scene
currentImageView.fitWidthProperty().bind(scene.widthProperty());
Next, let's cover JavaFX's native drag-and-drop support, which provides many op-
tions for users, such as dragging visual objects from an application to be dropped into
another application. In this scenario, the user will be dragging an image file from the
host windowing operating system to the image viewer application. In this scenario,
EventHandler objects must be generated to listen to DragEvent s. To fulfill this
requirement, you'll set up a scene's drag-over and drag-dropped event handler meth-
ods.
To set up the drag-over attribute, call the scene's setOnDragOver() method
with the appropriate generic EventHandler<DragEvent> type. In the example, a
lambda expression is used to implement the event handler. Implement the handle()
method via the lambda expression to listen to the drag-over event ( DragEvent ). In
the event handler , notice the event ( DragEvent ) object's invocation to the
getDragboard() method. The call to getDragboard() will return the drag
source ( Dragboard ), better known as the clipboard . Once the Dragboard object is
obtained, it is possible to determine and validate what is being dragged over the sur-
face. In this scenario, you need to determine whether the Dragboard object contains
any files. If it does, you call the event object's acceptTransferModes() by
passing in the constant TransferMode.COPY to provide visual feedback to the user
of the application (refer to Figure 15-2 ) . Otherwise, it should consume the event by
calling the event.consume() method. The following code demonstrates setting up
a scene's OnDragOver attribute:
// Dragging over surface
scene.setOnDragOver((DragEvent event) -> {
Dragboard db = event.getDragboard();
if (db.hasFiles()) {
event.acceptTransferModes(TransferMode.COPY);
} else {
event.consume();
}
});
Search WWH ::




Custom Search