Example usage for javafx.scene.input Dragboard getString

List of usage examples for javafx.scene.input Dragboard getString

Introduction

In this page you can find the example usage for javafx.scene.input Dragboard getString.

Prototype

public final String getString() 

Source Link

Document

Gets the plain text String from the clipboard which had previously been registered.

Usage

From source file:eu.over9000.skadi.ui.MainWindow.java

@Override
public void start(final Stage stage) throws Exception {
    this.stage = stage;

    this.detailPane = new ChannelDetailPane(this);

    this.bp = new BorderPane();
    this.sp = new SplitPane();
    this.sb = new StatusBar();

    this.setupTable();
    this.setupToolbar(stage);

    this.sp.getItems().add(this.table);

    this.bp.setTop(this.tb);
    this.bp.setCenter(this.sp);
    this.bp.setBottom(this.sb);

    final Scene scene = new Scene(this.bp, 1280, 720);
    scene.getStylesheets().add(this.getClass().getResource("/styles/copyable-label.css").toExternalForm());
    scene.setOnDragOver(event -> {//from  w  w  w . j a va 2  s .  c  o m
        final Dragboard d = event.getDragboard();
        if (d.hasUrl() || d.hasString()) {
            event.acceptTransferModes(TransferMode.COPY);
        } else {
            event.consume();
        }
    });
    scene.setOnDragDropped(event -> {
        final Dragboard d = event.getDragboard();
        boolean success = false;
        if (d.hasUrl()) {
            final String user = StringUtil.extractUsernameFromURL(d.getUrl());
            if (user != null) {
                success = this.channelHandler.addChannel(user, this.sb);
            } else {
                this.sb.setText("dragged url is no twitch stream");
            }
        } else if (d.hasString()) {
            success = this.channelHandler.addChannel(d.getString(), this.sb);
        }
        event.setDropCompleted(success);
        event.consume();

    });

    stage.setTitle("Skadi");
    stage.getIcons().add(new Image(this.getClass().getResourceAsStream("/icons/skadi.png")));
    stage.setScene(scene);
    stage.show();

    stage.iconifiedProperty().addListener((obs, oldV, newV) -> {
        if (this.currentState.isMinimizeToTray()) {
            if (newV) {
                stage.hide();
            }
        }
    });
    stage.setOnCloseRequest(event -> Platform.exit());

    this.bindColumnWidths();

}

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);/*from  w  w w . ja  v a2  s  .  c o m*/

            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 w w w  .j  a  va2 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();
}