package javafx_draganddrop; import java.io.File; import javafx.application.Application; import javafx.event.EventHandler; 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.TransferMode; import javafx.scene.layout.HBox; import javafx.stage.Stage; /** * * @web http://java-buddy.blogspot.com/ */ public class JavaFX_DragAndDrop extends Application { /** * @param args the command line arguments */ public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) { primaryStage.setTitle("java-buddy.blogspot.com"); Group root = new Group(); Scene scene = new Scene(root, 350, 300); HBox hBox = new HBox(); setupGestureTarget(scene, hBox); root.getChildren().add(hBox); primaryStage.setScene(scene); primaryStage.show(); primaryStage.show(); } void setupGestureTarget(final Scene target, final HBox targetBox){ target.setOnDragOver(new EventHandler <DragEvent>() { @Override public void handle(DragEvent event) { /* data is dragged over the target */ System.out.println("onDragOver"); Dragboard db = event.getDragboard(); if(db.hasFiles()){ event.acceptTransferModes(TransferMode.COPY); } event.consume(); } }); target.setOnDragDropped(new EventHandler <DragEvent>() { @Override public void handle(DragEvent event) { /* data dropped */ System.out.println("onDragDropped"); Dragboard db = event.getDragboard(); if(db.hasFiles()){ for(File file:db.getFiles()){ String absolutePath = file.getAbsolutePath(); Image dbimage = new Image(absolutePath); ImageView dbImageView = new ImageView(); dbImageView.setImage(dbimage); targetBox.getChildren().add(dbImageView); } event.setDropCompleted(true); }else{ event.setDropCompleted(false); } event.consume(); } }); } }
Related article:
- JavaFX 2.0: drag-and-drop gesture for text
- JavaFX 2.0: Implement Source and Target of Drag and Drop for Image
Nice Example..!!
ReplyDeletehow to embed the internet radio in standalone application using javaFX
ReplyDeletefile.getAbsolutePath();
ReplyDeletedoes not work because there isn't the protocol 'file'
you try
file.toURI().toURL().toString();
i try String absolutePath = file.toURI().toString(); and works perfectly my friend! thank you!
Delete