Wednesday, March 14, 2012

JavaFX 2.0: Implement GestureTarget of Drag and Drop for Image

This article demonstrate how to implement GestureTarget of Drag and Drop for Image. When a image is drag over and drop on the scene, it will be copy in a ImageView, and add to a HBox in the scene.


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

4 comments:

  1. how to embed the internet radio in standalone application using javaFX

    ReplyDelete
  2. file.getAbsolutePath();

    does not work because there isn't the protocol 'file'

    you try
    file.toURI().toURL().toString();

    ReplyDelete
    Replies
    1. i try String absolutePath = file.toURI().toString(); and works perfectly my friend! thank you!

      Delete