Example usage for javafx.scene.input TransferMode MOVE

List of usage examples for javafx.scene.input TransferMode MOVE

Introduction

In this page you can find the example usage for javafx.scene.input TransferMode MOVE.

Prototype

TransferMode MOVE

To view the source code for javafx.scene.input TransferMode MOVE.

Click Source Link

Document

Indicates moving of data is supported or intended.

Usage

From source file:Main.java

private void initializeListeners() {
    // drag from left to right
    leftListView.setOnDragDetected(new EventHandler<MouseEvent>() {
        @Override//from   w w  w. j a  v  a 2s .  com
        public void handle(MouseEvent event) {
            if (leftListView.getSelectionModel().getSelectedItem() == null) {
                return;
            }

            Dragboard dragBoard = leftListView.startDragAndDrop(TransferMode.MOVE);
            ClipboardContent content = new ClipboardContent();
            content.putString(leftListView.getSelectionModel().getSelectedItem().getName());
            dragBoard.setContent(content);
        }
    });

    rightListView.setOnDragOver(new EventHandler<DragEvent>() {
        @Override
        public void handle(DragEvent dragEvent) {
            dragEvent.acceptTransferModes(TransferMode.MOVE);
        }
    });

    rightListView.setOnDragDropped(new EventHandler<DragEvent>() {
        @Override
        public void handle(DragEvent dragEvent) {
            String player = dragEvent.getDragboard().getString();
            rightListView.getItems().addAll(new Student(player));
            leftList.remove(new Student(player));
            leftListView.setItems(leftList);
            dragEvent.setDropCompleted(true);
        }
    });
    // drag from right to left
    rightListView.setOnDragDetected(new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent event) {
            Dragboard dragBoard = rightListView.startDragAndDrop(TransferMode.MOVE);
            ClipboardContent content = new ClipboardContent();
            content.putString(rightListView.getSelectionModel().getSelectedItem().getName());
            dragBoard.setContent(content);
        }
    });

    leftListView.setOnDragOver(new EventHandler<DragEvent>() {
        @Override
        public void handle(DragEvent dragEvent) {
            dragEvent.acceptTransferModes(TransferMode.MOVE);
        }
    });

    leftListView.setOnDragDropped(new EventHandler<DragEvent>() {
        @Override
        public void handle(DragEvent dragEvent) {
            String player = dragEvent.getDragboard().getString();
            leftListView.getItems().remove(new Student(player));

            rightList.remove(new Student(player));
            dragEvent.setDropCompleted(true);
        }
    });
}

From source file:Main.java

@Override
public void start(Stage primaryStage) {
    Group root = new Group();
    Scene scene = new Scene(root, 300, 250);
    root.getChildren().addAll(source, target);

    source.setOnDragDetected(new EventHandler<MouseEvent>() {
        public void handle(MouseEvent event) {
            /* drag was detected, start a drag-and-drop gesture*/
            /* allow any transfer mode */
            Dragboard db = source.startDragAndDrop(TransferMode.ANY);

            /* Put a string on a dragboard */
            ClipboardContent content = new ClipboardContent();
            content.putString(source.getText());
            db.setContent(content);/*w  ww .  j  ava2  s  . c om*/

            event.consume();
        }
    });
    target.setOnDragOver(new EventHandler<DragEvent>() {
        public void handle(DragEvent event) {
            /* data is dragged over the target */
            /* accept it only if it is not dragged from the same node 
             * and if it has a string data */
            if (event.getGestureSource() != target && event.getDragboard().hasString()) {
                /* allow for moving */
                event.acceptTransferModes(TransferMode.MOVE);
            }

            event.consume();
        }
    });
    target.setOnDragEntered(new EventHandler<DragEvent>() {
        public void handle(DragEvent event) {
            /* the drag-and-drop gesture entered the target */
            /* show to the user that it is an actual gesture target */
            if (event.getGestureSource() != target && event.getDragboard().hasString()) {
                target.setFill(Color.GREEN);
            }

            event.consume();
        }
    });
    target.setOnDragExited(new EventHandler<DragEvent>() {
        public void handle(DragEvent event) {
            /* mouse moved away, remove the graphical cues */
            target.setFill(Color.BLACK);

            event.consume();
        }
    });
    target.setOnDragDropped(new EventHandler<DragEvent>() {
        public void handle(DragEvent event) {
            /* data dropped */
            /* if there is a string data on dragboard, read it and use it */
            Dragboard db = event.getDragboard();
            boolean success = false;
            if (db.hasString()) {
                target.setText(db.getString());
                success = true;
            }
            /* let the source know whether the string was successfully 
             * transferred and used */
            event.setDropCompleted(success);

            event.consume();
        }
    });
    source.setOnDragDone(new EventHandler<DragEvent>() {
        public void handle(DragEvent event) {
            /* the drag and drop gesture ended */
            /* if the data was successfully moved, clear it */
            if (event.getTransferMode() == TransferMode.MOVE) {
                source.setText("");
            }
            event.consume();
        }
    });

    primaryStage.setScene(scene);
    primaryStage.show();
}

From source file:Main.java

@Override
public void start(Stage stage) {
    stage.setTitle("Hello Drag And Drop");

    Group root = new Group();
    Scene scene = new Scene(root, 400, 200);
    scene.setFill(Color.LIGHTGREEN);

    final Text source = new Text(50, 100, "DRAG ME");
    source.setScaleX(2.0);//from ww w  .ja  v  a  2 s  .  c  o m
    source.setScaleY(2.0);

    final Text target = new Text(250, 100, "DROP HERE");
    target.setScaleX(2.0);
    target.setScaleY(2.0);

    source.setOnDragDetected(new EventHandler<MouseEvent>() {
        public void handle(MouseEvent event) {
            /* drag was detected, start drag-and-drop gesture*/
            System.out.println("onDragDetected");

            /* allow any transfer mode */
            Dragboard db = source.startDragAndDrop(TransferMode.ANY);

            /* put a string on dragboard */
            ClipboardContent content = new ClipboardContent();
            content.putString(source.getText());
            db.setContent(content);

            event.consume();
        }
    });

    target.setOnDragOver(new EventHandler<DragEvent>() {
        public void handle(DragEvent event) {
            /* data is dragged over the target */
            System.out.println("onDragOver");

            /* accept it only if it is  not dragged from the same node 
             * and if it has a string data */
            if (event.getGestureSource() != target && event.getDragboard().hasString()) {
                /* allow for both copying and moving, whatever user chooses */
                event.acceptTransferModes(TransferMode.COPY_OR_MOVE);
            }

            event.consume();
        }
    });

    target.setOnDragEntered(new EventHandler<DragEvent>() {
        public void handle(DragEvent event) {
            /* the drag-and-drop gesture entered the target */
            System.out.println("onDragEntered");
            /* show to the user that it is an actual gesture target */
            if (event.getGestureSource() != target && event.getDragboard().hasString()) {
                target.setFill(Color.GREEN);
            }

            event.consume();
        }
    });

    target.setOnDragExited(new EventHandler<DragEvent>() {
        public void handle(DragEvent event) {
            /* mouse moved away, remove the graphical cues */
            target.setFill(Color.BLACK);

            event.consume();
        }
    });

    target.setOnDragDropped(new EventHandler<DragEvent>() {
        public void handle(DragEvent event) {
            /* data dropped */
            System.out.println("onDragDropped");
            /* if there is a string data on dragboard, read it and use it */
            Dragboard db = event.getDragboard();
            boolean success = false;
            if (db.hasString()) {
                target.setText(db.getString());
                success = true;
            }
            /* let the source know whether the string was successfully 
             * transferred and used */
            event.setDropCompleted(success);

            event.consume();
        }
    });

    source.setOnDragDone(new EventHandler<DragEvent>() {
        public void handle(DragEvent event) {
            /* the drag-and-drop gesture ended */
            System.out.println("onDragDone");
            /* if the data was successfully moved, clear it */
            if (event.getTransferMode() == TransferMode.MOVE) {
                source.setText("");
            }

            event.consume();
        }
    });

    root.getChildren().add(source);
    root.getChildren().add(target);
    stage.setScene(scene);
    stage.show();
}

From source file:dpfmanager.shell.interfaces.gui.component.dessign.DessignView.java

private void addConfigFiles() {
    String previous = null;//from  w  ww.  j  a va 2  s. c om
    if (selectedButton != null) {
        previous = selectedButton.getText();
    }
    selectedButton = null;
    group = new ToggleGroup();
    vBoxConfig = new VBox();
    vBoxConfig.setId("vBoxConfig");
    vBoxConfig.setSpacing(3);
    vBoxConfig.setPadding(new Insets(5));

    // Default one
    String description = interService.getDescriptionFromDefault();
    if (description != null) {
        addConfigFile(bundle.getString("default"), description,
                bundle.getString("default").equalsIgnoreCase(previous));
    }

    // User configs
    File folder = new File(DPFManagerProperties.getConfigDir());
    for (final File fileEntry : folder.listFiles()) {
        if (fileEntry.isFile()) {
            if (fileEntry.getName().toLowerCase().endsWith(".dpf")) {
                description = getController().readDescription(fileEntry);
                addConfigFile(fileEntry.getName(), description, fileEntry.getName().equalsIgnoreCase(previous));
            }
        }
    }

    // Drag and drop
    configScroll.setOnDragDropped(event -> {
        // Files dropped
        Dragboard db = event.getDragboard();
        boolean success = false;
        if (db.hasFiles()) {
            success = true;
            for (File file : db.getFiles()) {
                getController().addConfigFile(file, false);
            }
        }
        event.setDropCompleted(success);
        event.consume();
    });
    configScroll.setOnDragOver(event -> {
        if (acceptedFiles(event.getDragboard(), Arrays.asList("dpf"), false)) {
            event.acceptTransferModes(TransferMode.MOVE);
        }
        event.consume();
    });
    configScroll.setOnDragEntered(event -> {
        if (acceptedFiles(event.getDragboard(), Arrays.asList("dpf"), false)) {
            configScroll.getStyleClass().add("on-drag");
        }
        event.consume();
    });
    configScroll.setOnDragExited(event -> {
        configScroll.getStyleClass().remove("on-drag");
        event.consume();
    });

    configScroll.setContent(vBoxConfig);
}

From source file:dpfmanager.shell.interfaces.gui.component.dessign.DessignView.java

@FXML
protected void onDragOverInput(DragEvent event) throws Exception {
    // Filter accepted files
    if (acceptedFiles(event.getDragboard(), getController().getAcceptedExetsniosn(), true)) {
        event.acceptTransferModes(TransferMode.MOVE);
    }//from  w  w  w.  j a  va2  s  .co  m
    event.consume();
}