Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: javafx problem

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default javafx problem

    below is my code for drag and drop and image for er diagram maker in java fx.. but image is dragging only once.. how to make it drag and drop multiple tyms and also how to save the dragged image... plz help me our as soon as possible






     
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package ads;
     
    import java.io.File;
    import java.net.URL;
    import java.util.ResourceBundle;
    import javafx.embed.swing.SwingFXUtils;
    import javafx.event.ActionEvent;
    import javafx.fxml.FXML;
    import javafx.fxml.Initializable;
    import javafx.scene.Cursor;
    import javafx.scene.Group;
    import javafx.scene.Node;
    import javafx.scene.control.Button;
    import javafx.scene.control.ScrollPane;
    import javafx.scene.image.ImageView;
    import javafx.scene.image.WritableImage;
    import javafx.scene.input.*;
    import javafx.scene.layout.AnchorPane;
    import javafx.scene.text.Text;
    import javax.imageio.ImageIO;
     
     
    public class SampleController1 implements Initializable {
     
       // @FXML
       // public Button all[]=new Button[100]; 
        @FXML
           public Button entity;
        @FXML
        public Button attribute;
        @FXML
        public ScrollPane draw;
        @FXML
        public AnchorPane test;
     
     
        Group root = new Group(//<editor-fold defaultstate="collapsed" desc="comment">
     
                //</editor-fold>
    );
     
     
     
     
     
     
     
        ImageView[] img={new ImageView("ads/icons/ent.png"),new ImageView("ads/icons/att.png"),new ImageView("ads/icons/rel.png")};
     
     
        ImageView[] imgtest=new ImageView[50];
     
     
     
        boolean flag=false;
        private Node[] children;
     
     
    //boolean test=false;
        @FXML
        public void handcursor(MouseEvent event) {
            entity.setCursor(Cursor.HAND);
        }
     
        /**
         *
         * @param event
         */
        @FXML
        public void handleButtonAction(ActionEvent event) {
            System.out.println("You clicked me!dfgfdgdf");
        }
     
        @FXML
        public void OnDragDetected(MouseEvent event) {
            System.out.println("onDragDetected");
            Dragboard db = entity.startDragAndDrop(TransferMode.ANY);   
     
     
            ClipboardContent content = new ClipboardContent();
            content.putImage(img[0].getImage());
     
     
            db.setContent(content);
     
            event.consume();
     
     
        }
     
        @FXML
        public void OnDragDetectedPane(MouseEvent event) {
     
     
     
     
            System.out.println("onDragDetected");
            Dragboard db = entity.startDragAndDrop(TransferMode.ANY);    
           db.getTransferModes();
            ClipboardContent content = new ClipboardContent();
           content.putImage(img[0].getImage());
            db.setContent(content);
            event.consume();
     
     
     
        }
     
     
     
     
     
     
     
     
        @FXML
        public void OnDragDone(DragEvent event) {
     
            System.out.println("onDragDone");
            if (event.getTransferMode() == TransferMode.MOVE) {
                System.out.println("hello");
     
     
     
     
            }
            event.consume();
     
        }
     
          @FXML
     
        public void OnDragDonePane(DragEvent event) {
     
            System.out.println("onDragDone");
            if (event.getTransferMode() == TransferMode.MOVE) {
                System.out.println("hello");
     
     
     
     
            }
     
            event.consume();
     
     
          }
     
     
     
        @FXML
        public void OnDragDropped(DragEvent event) {
            System.out.println("onDragDropped");
     
            boolean success = false;
            Dragboard db = event.getDragboard();
     
            ClipboardContent content = new ClipboardContent();
            if (flag==true) {
                //img.setFitHeight(event.getSceneY());
                //img.setFitWidth(event.getSceneX());
                success=true;
                flag=false;
     
            }
     
     
            event.setDropCompleted(success);
            event.consume();
     
        }
     
     
          @FXML
        public void OnDragDroppedPane(DragEvent event) {
            System.out.println("onDragDropped");
     
            boolean success = false;
            Dragboard db = event.getDragboard();
            ClipboardContent content = new ClipboardContent();
            if (flag==true) {
                //img.setFitHeight(event.getSceneY());
                //img.setFitWidth(event.getSceneX());
                success=true;
                flag=false;
     
            }
     
     
     
            event.setDropCompleted(success);
            event.consume();
     
        }
     
     
     
     
     
     
        @FXML
        public Button temp;
     
        @FXML
        public Text t=new Text("Hello");
     
     
     
        @FXML
        public void OnDragOver(DragEvent event) {
            System.out.println("onDragOver");
            if (event.getGestureSource() != draw
                    && event.getDragboard().hasImage()) {
                event.acceptTransferModes(TransferMode.COPY_OR_MOVE);
            }
            Dragboard db = event.getDragboard();
            //boolean success = false;
            if (db.hasImage()) {
                System.out.println("M in");
                img[0].setLayoutX(event.getX());
                img[0].setLayoutY(event.getY());
                img[0].setImage(db.getImage());
                draw.setContent(img[0]);
                flag=true;
                //success = true;
            }
            //event.setDropCompleted(success);
            event.consume();
        }
     
     
     
        public void OnDragOverPane(DragEvent event) {
            System.out.println("onDragOver");
            if (event.getGestureSource() != draw
                    && event.getDragboard().hasImage()) {
                event.acceptTransferModes(TransferMode.COPY_OR_MOVE);
            }
            Dragboard db = event.getDragboard();
            //boolean success = false;
            if (db.hasImage()) {
                System.out.println("M in");
                img[0].setLayoutX(event.getX());
                img[0].setLayoutY(event.getY());
                img[0].setImage(db.getImage());
                draw.setContent(img[0]);
     
                flag=true;
                //success = true;
            }
            WritableImage wim = new WritableImage(300, 250);
            img[1].snapshot(null, wim);
     
      File file = new File("ImageWithEffect.png");
     
     
     
            try {
                ImageIO.write(SwingFXUtils.fromFXImage(wim, null), "png", file);
            } catch (Exception s) {
            }
            //event.setDropCompleted(success);
            event.consume();
        }
     
     
     
     
     
        @Override
        public void initialize(URL url, ResourceBundle rb) {
            // TODO
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,618
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: javafx problem

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: javafx problem

     
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package ads;
     
    import java.io.File;
    import java.net.URL;
    import java.util.ResourceBundle;
    import javafx.embed.swing.SwingFXUtils;
    import javafx.event.ActionEvent;
    import javafx.fxml.FXML;
    import javafx.fxml.Initializable;
    import javafx.scene.Cursor;
    import javafx.scene.Group;
    import javafx.scene.Node;
    import javafx.scene.control.Button;
    import javafx.scene.control.ScrollPane;
    import javafx.scene.image.ImageView;
    import javafx.scene.image.WritableImage;
    import javafx.scene.input.*;
    import javafx.scene.layout.AnchorPane;
    import javafx.scene.text.Text;
    import javax.imageio.ImageIO;
     
     
    public class SampleController1 implements Initializable {
     
       // @FXML
       // public Button all[]=new Button[100]; 
        @FXML
           public Button entity;
        @FXML
        public Button attribute;
        @FXML
        public ScrollPane draw;
        @FXML
        public AnchorPane test;
     
     
        Group root = new Group(//<editor-fold defaultstate="collapsed" desc="comment">
     
                //</editor-fold>
    );
     
     
     
     
     
     
     
        ImageView[] img={new ImageView("ads/icons/ent.png"),new ImageView("ads/icons/att.png"),new ImageView("ads/icons/rel.png")};
     
     
        ImageView[] imgtest=new ImageView[50];
     
     
     
        boolean flag=false;
        private Node[] children;
     
     
    //boolean test=false;
        @FXML
        public void handcursor(MouseEvent event) {
            entity.setCursor(Cursor.HAND);
        }
     
        /**
         *
         * @param event
         */
        @FXML
        public void handleButtonAction(ActionEvent event) {
            System.out.println("You clicked me!dfgfdgdf");
        }
     
        @FXML
        public void OnDragDetected(MouseEvent event) {
            System.out.println("onDragDetected");
            Dragboard db = entity.startDragAndDrop(TransferMode.ANY);   
     
     
            ClipboardContent content = new ClipboardContent();
            content.putImage(img[0].getImage());
     
     
            db.setContent(content);
     
            event.consume();
     
     
        }
     
        @FXML
        public void OnDragDetectedPane(MouseEvent event) {
     
     
     
     
            System.out.println("onDragDetected");
            Dragboard db = entity.startDragAndDrop(TransferMode.ANY);    
           db.getTransferModes();
            ClipboardContent content = new ClipboardContent();
           content.putImage(img[0].getImage());
            db.setContent(content);
            event.consume();
     
     
     
        }
     
     
     
     
     
     
     
     
        @FXML
        public void OnDragDone(DragEvent event) {
     
            System.out.println("onDragDone");
            if (event.getTransferMode() == TransferMode.MOVE) {
                System.out.println("hello");
     
     
     
     
            }
            event.consume();
     
        }
     
          @FXML
     
        public void OnDragDonePane(DragEvent event) {
     
            System.out.println("onDragDone");
            if (event.getTransferMode() == TransferMode.MOVE) {
                System.out.println("hello");
     
     
     
     
            }
     
            event.consume();
     
     
          }
     
     
     
        @FXML
        public void OnDragDropped(DragEvent event) {
            System.out.println("onDragDropped");
     
            boolean success = false;
            Dragboard db = event.getDragboard();
     
            ClipboardContent content = new ClipboardContent();
            if (flag==true) {
                //img.setFitHeight(event.getSceneY());
                //img.setFitWidth(event.getSceneX());
                success=true;
                flag=false;
     
            }
     
     
            event.setDropCompleted(success);
            event.consume();
     
        }
     
     
          @FXML
        public void OnDragDroppedPane(DragEvent event) {
            System.out.println("onDragDropped");
     
            boolean success = false;
            Dragboard db = event.getDragboard();
            ClipboardContent content = new ClipboardContent();
            if (flag==true) {
                //img.setFitHeight(event.getSceneY());
                //img.setFitWidth(event.getSceneX());
                success=true;
                flag=false;
     
            }
     
     
     
            event.setDropCompleted(success);
            event.consume();
     
        }
     
     
     
     
     
     
        @FXML
        public Button temp;
     
        @FXML
        public Text t=new Text("Hello");
     
     
     
        @FXML
        public void OnDragOver(DragEvent event) {
            System.out.println("onDragOver");
            if (event.getGestureSource() != draw
                    && event.getDragboard().hasImage()) {
                event.acceptTransferModes(TransferMode.COPY_OR_MOVE);
            }
            Dragboard db = event.getDragboard();
            //boolean success = false;
            if (db.hasImage()) {
                System.out.println("M in");
                img[0].setLayoutX(event.getX());
                img[0].setLayoutY(event.getY());
                img[0].setImage(db.getImage());
                draw.setContent(img[0]);
                flag=true;
                //success = true;
            }
            //event.setDropCompleted(success);
            event.consume();
        }
     
     
     
        public void OnDragOverPane(DragEvent event) {
            System.out.println("onDragOver");
            if (event.getGestureSource() != draw
                    && event.getDragboard().hasImage()) {
                event.acceptTransferModes(TransferMode.COPY_OR_MOVE);
            }
            Dragboard db = event.getDragboard();
            //boolean success = false;
            if (db.hasImage()) {
                System.out.println("M in");
                img[0].setLayoutX(event.getX());
                img[0].setLayoutY(event.getY());
                img[0].setImage(db.getImage());
                draw.setContent(img[0]);
     
                flag=true;
                //success = true;
            }
            WritableImage wim = new WritableImage(300, 250);
            img[1].snapshot(null, wim);
     
      File file = new File("ImageWithEffect.png");
     
     
     
            try {
                ImageIO.write(SwingFXUtils.fromFXImage(wim, null), "png", file);
            } catch (Exception s) {
            }
            //event.setDropCompleted(success);
            event.consume();
        }
     
     
     
     
     
        @Override
        public void initialize(URL url, ResourceBundle rb) {
            // TODO
        }
    }

Similar Threads

  1. How do I inherit values in JavaFX?
    By Leprechaun_hunter in forum Object Oriented Programming
    Replies: 0
    Last Post: December 2nd, 2012, 03:17 PM
  2. from java to javaFX
    By johagood in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 8th, 2012, 02:38 PM
  3. JavaFX on javaprogrammingforums
    By cselic in forum Forum Updates & Feedback
    Replies: 1
    Last Post: July 31st, 2012, 03:48 PM
  4. Questions about javafx
    By mDennis10 in forum Java Theory & Questions
    Replies: 3
    Last Post: September 5th, 2011, 12:19 AM
  5. [SOLVED] JavaFx
    By chronoz13 in forum Java Theory & Questions
    Replies: 2
    Last Post: August 28th, 2009, 09:13 PM