2 Replies Latest reply on Dec 20, 2012 1:17 AM by 976866

    drag the image into target

      I need to drag the image like (banana.png) and drop that image into target(rectangle),target2(rectangle),etc. but I difficult to replace that target(rectangle) to be (banana.png) in drop event handler, Please help Me..

      this is my source code

      final Rectangle target = new Rectangle();

      target.setOnDragDropped(new EventHandler <DragEvent>() {

      public void handle(DragEvent event) {
      Dragboard db = event.getDragboard();

      if (db.hasImage()) {
      db.getImage(); //how to get the image (banana.png) and replace the target (rectangle) ?




        • 1. Re: drag the image into target
          final Pane rectangleHolder = new Pane();
          final Rectangle rectangle = new Rectangle();
          // add rectangleHolder to the layout instead of rectangle...
          // and then
          if (db.hasImage()) {
            rectangleHolder.getChildren().setAll(new ImageView(db.getImage());
          Depending on your layout, you may be able simply replace the rectangle in the existing layout without introducing the new Pane. You can always do something like
          ObservableList<Node> rectangleAndSiblings = ((Pane)rectangle.getParent()).getChildren();
          rectangleAndSiblings.set(rectangleAndSiblings.indexOf(rectangle), new ImageView(db.getImage()));
          but it doesn't need to be that complex.
          1 person found this helpful
          • 2. Re: drag the image into target
            thank you James D, it's so helpful