JavaFX Scene handle drag and drop event
import java.io.File; import java.util.ArrayList; import java.util.List; import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.input.DragEvent; import javafx.scene.input.Dragboard; import javafx.scene.input.MouseEvent; import javafx.scene.input.TransferMode; import javafx.scene.layout.HBox; import javafx.scene.paint.Color; import javafx.scene.shape.Arc; import javafx.scene.shape.ArcType; import javafx.scene.shape.Rectangle; import javafx.stage.Stage; public class Main extends Application { private final List<String> imageFiles = new ArrayList<>(); private int currentIndex = -1; private final String filePrefix = "file:"; public static void main(String[] args) { Application.launch(args);//from w w w . ja va 2s.co m } @Override public void start(Stage primaryStage) { primaryStage.setTitle("Creating a Image"); Group root = new Group(); Scene scene = new Scene(root, 551, 400, Color.BLACK); final ImageView currentImageView = new ImageView(); currentImageView.setPreserveRatio(true); currentImageView.fitWidthProperty().bind(scene.widthProperty()); final HBox pictureRegion = new HBox(); pictureRegion.getChildren().add(currentImageView); root.getChildren().add(pictureRegion); scene.setOnDragOver((DragEvent event) -> { Dragboard db = event.getDragboard(); if (db.hasFiles()) { event.acceptTransferModes(TransferMode.COPY); } else { event.consume(); } }); scene.setOnDragDropped((DragEvent event) -> { Dragboard db = event.getDragboard(); boolean success = false; if (db.hasFiles()) { success = true; String filePath = null; for (File file : db.getFiles()) { filePath = file.getAbsolutePath(); currentIndex += 1; imageFiles.add(currentIndex, filePath); } filePath = filePrefix + filePath; Image imageimage = new Image(filePath); currentImageView.setImage(imageimage); } event.setDropCompleted(success); event.consume(); }); Group buttonGroup = new Group(); Rectangle buttonArea = new Rectangle(); buttonArea.setArcWidth(15); buttonArea.setArcHeight(20); buttonArea.setFill(new Color(0, 0, 0, .55)); buttonArea.setX(0); buttonArea.setY(0); buttonArea.setWidth(60); buttonArea.setHeight(30); buttonArea.setStroke(Color.rgb(255, 255, 255, .70)); buttonGroup.getChildren().add(buttonArea); Arc leftButton = new Arc(); leftButton.setType(ArcType.ROUND); leftButton.setCenterX(12); leftButton.setCenterY(16); leftButton.setRadiusX(15); leftButton.setRadiusY(15); leftButton.setStartAngle(-30); leftButton.setLength(60); leftButton.setFill(new Color(1, 1, 1, .90)); leftButton.addEventHandler(MouseEvent.MOUSE_PRESSED, (MouseEvent me) -> { int indx = gotoImageIndex(ButtonMove.PREV); if (indx > -1) { String namePict = imageFiles.get(indx); namePict = filePrefix + namePict; final Image image = new Image(namePict); currentImageView.setImage(image); } }); buttonGroup.getChildren().add(leftButton); Arc rightButton = new Arc(); rightButton.setType(ArcType.ROUND); rightButton.setCenterX(12); rightButton.setCenterY(16); rightButton.setRadiusX(15); rightButton.setRadiusY(15); rightButton.setStartAngle(180 - 30); rightButton.setLength(60); rightButton.setFill(new Color(1, 1, 1, .90)); rightButton.setTranslateX(40); buttonGroup.getChildren().add(rightButton); rightButton.addEventHandler(MouseEvent.MOUSE_PRESSED, (MouseEvent me) -> { int indx = gotoImageIndex(ButtonMove.NEXT); if (indx > -1) { String namePict = imageFiles.get(indx); namePict = filePrefix + namePict; final Image image = new Image(namePict); currentImageView.setImage(image); } }); buttonGroup.translateXProperty().bind(scene.widthProperty().subtract(buttonArea.getWidth() + 6)); buttonGroup.translateYProperty().bind(scene.heightProperty().subtract(buttonArea.getHeight() + 6)); root.getChildren().add(buttonGroup); primaryStage.setScene(scene); primaryStage.show(); } public int gotoImageIndex(ButtonMove direction) { int size = imageFiles.size(); if (size == 0) { currentIndex = -1; } else if (direction == ButtonMove.NEXT && size > 1 && currentIndex < size - 1) { currentIndex += 1; } else if (direction == ButtonMove.PREV && size > 1 && currentIndex > 0) { currentIndex -= 1; } return currentIndex; } } enum ButtonMove { NEXT, PREV };