2 Replies Latest reply: Dec 19, 2012 7:17 PM by 976866 RSS

    drag the image into target

    976866
      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>() {

      @Override
      public void handle(DragEvent event) {
      System.out.println("onDragDrop");
      Dragboard db = event.getDragboard();

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

      }

      event.consume();

      }

      });
        • 1. Re: drag the image into target
          James_D
          final Pane rectangleHolder = new Pane();
          final Rectangle rectangle = new Rectangle();
          rectangleHolder.getChildren().add(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.
          • 2. Re: drag the image into target
            976866
            thank you James D, it's so helpful