1

I have a Tilepane(source) which loaded the images i stored in database, I would like to drag any images loaded in my Tilepane and drop it to some Imageview(target) which i provided in the FXML scene builder.

left:source , right:Target

I was getting trouble in startDragAndDrop(TransferMode.MOVE) because my the image in my source is not not final imageview as they load from databases.

I would need an example of how to deal with dropping of images into an JavaFX ImageView(target) from my Tilepane(source).

Thank and best regards, Steps.

  try {  

        Try2.pstmtGetAlbumPhoto = Try2.getConnection().prepareStatement(RetrievePhoto);
        ResultSet RSphoto = Try2.pstmtGetAlbumPhoto.executeQuery();

        while(RSphoto.next()){      
            imageView = new ImageView();
            imageView.setFitWidth(120);
            imageView.setFitHeight(70);          
            String Path = RSphoto.getString("Path");                   
            System.out.println(Path);    
            BufferedImage images = null;

            try {    
                images = ImageIO.read(new File(Path));                
             } catch (IOException ex) {
                System.out.println("Image failed to load.");
             }
            WritableImage wr = null;
            if (images != null) {
                wr = new WritableImage(images.getWidth(), images.getHeight());
                PixelWriter pw = wr.getPixelWriter();
                for (int x = 0; x < images.getWidth(); x++) 
                {
                    for (int y = 0; y < images.getHeight(); y++) {
                    pw.setArgb(x, y, images.getRGB(x, y));
                    }
                }
            }    
    imageView.setImage(wr); 

    final ImageView currentImage = new ImageView();
    currentImage.setImage(wr);
    currentImage.setId(this.getClass().getSimpleName() + System.currentTimeMillis());


    imageView.setOnDragDetected(new EventHandler<MouseEvent>() {
    public void handle(MouseEvent event) {

    Dragboard db = currentImage.startDragAndDrop(TransferMode.ANY);
    ClipboardContent content = new ClipboardContent();
    content.putImage(currentImage.getImage());
    content.putString(getName());
    db.setContent(content);
    event.consume();
    }
    });  

    target.setOnDragOver(new EventHandler<DragEvent>() {
    public void handle(DragEvent event) {
    Dragboard db = event.getDragboard();
    if (db.hasImage() || db.hasFiles()) {
            event.acceptTransferModes(TransferMode.COPY);
        }  
    }
    });    

    target.setOnDragDropped(new EventHandler<DragEvent>() {
        public void handle(DragEvent event) {
            Dragboard db = event.getDragboard();
        if (db.hasImage()) {
            target.setImage(db.getImage());
            String x = db.getString();
            System.out.println(x);
            ;
            event.setDropCompleted(true);
        } 

    }
    });

    TilePane3.getChildren().addAll(imageView);  
    }      
    } catch (SQLException ex) {
        Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
   }
  • Can you post some code showing what you've tried? It looks like this might just be a duplicate of http://stackoverflow.com/questions/27592379/local-variables-referenced-from-a-lambda-expression-must-be-final-or-effectively – James_D Mar 15 '16 at 14:18
  • @James_D Hi, i have updated the code i am working on, i am getting error like {Exception in thread "JavaFX Application Thread" java.lang.IllegalStateException: Cannot start drag and drop on node that is not in scene} , and i got no idea how to fix that, hoping can get some suggestion =) – Stephenie Tan Mar 15 '16 at 15:43
  • Oracle produced a really nice sample [Paper Doll drag and drop application](https://docs.oracle.com/javase/8/javafx/events-tutorial/paper-doll.htm) with ImageViews, perhaps see if understanding the source for that helps you in solving your problem. – jewelsea Mar 15 '16 at 18:09
  • I think you just need `ImageView currentImage = imageView ;` instead of the `ImageView currentImage = new ImageView();` and `currentImage.setImage(wr);` – James_D Mar 15 '16 at 20:39
  • @jewelsea i have look into it, but i not really understand all of it, but i will try to go through it again , thanks for the sample -) – Stephenie Tan Mar 16 '16 at 04:51
  • @James_D oh god! it work perfectly now with the changes! Thanks you so much! – Stephenie Tan Mar 16 '16 at 04:52

0 Answers0