scala - JavaFX/ScalaFX 和剪贴板 : Cannot copy files?

标签 scala ubuntu javafx clipboard scalafx

复制文件不起作用:

  def toClipboard(selectedLinesOnly: Boolean = false): Unit = {
    val clipboard = Clipboard.systemClipboard
    val content = new ClipboardContent
    val items: Iterable[FileRecord] = selectedLinesOnly match {
      case true => tableView.selectionModel.value.selectedItems.toSeq
      case false => tableView.items.value
    }
    val files = items.map(_.file)
    println(s"COPY $files")
    content.putFiles(files.toSeq)
    clipboard.content = content
  }

输出:[info] COPY [SFX][/tmp/test/a.txt,/tmp/test/b.txt]

没有要粘贴的文件。

  def toClipboard(selectedLinesOnly: Boolean = false): Unit = {
    val clipboard = Clipboard.systemClipboard
    val content = new ClipboardContent
    val items: Iterable[FileRecord] = selectedLinesOnly match {
      case true => tableView.selectionModel.value.selectedItems.toSeq
      case false => tableView.items.value
    }
    val files = items.map(_.file.getPath)
    println(s"COPY $files")
    content.putFilesByPath(files.toSeq)
    clipboard.content = content
  }

输出:[info] COPY [SFX][/tmp/test/a.txt,/tmp/test/b.txt]

没有要粘贴的文件。

  def toClipboard(selectedLinesOnly: Boolean = false): Unit = {
    val clipboard = Clipboard.systemClipboard
    val content = new ClipboardContent
    val items: Iterable[FileRecord] = selectedLinesOnly match {
      case true => tableView.selectionModel.value.selectedItems.toSeq
      case false => tableView.items.value
    }
    val files = items.map("file://" + _.file.getPath)
    println(s"COPY $files")
    content.putFilesByPath(files.toSeq)
    clipboard.content = content
  }

输出:[info] COPY [SFX][file:///tmp/test/a.txt, file:///tmp/test/b.txt]

没有要粘贴的文件。

但是可以将路径复制到字符串剪贴板:

  def toClipboard(selectedLinesOnly: Boolean = false): Unit = {
    val clipboard = Clipboard.systemClipboard
    val content = new ClipboardContent
    val items: Iterable[FileRecord] = selectedLinesOnly match {
      case true => tableView.selectionModel.value.selectedItems.toSeq
      case false => tableView.items.value
    }
    val files = items.map(_.file.getPath)
    println(s"COPY $files")
    content.putString(files.mkString(" "))
    clipboard.content = content
  }

现在这是在我的剪贴板中:“/tmp/test/a.txt/tmp/test/b.txt”

但我需要文件形式的它,而不是字符串。

如何在我的应用程序中复制文件?

我正在 Ubuntu 上使用 OpenJFX 8。

最佳答案

ClipBoard 没有像 FileUtils.copyDirectoryToDirectoryFileUtils.moveDirectoryToDirectory (也称为复制或剪切)的功能。剪贴板只能提供路径或一般数据。使用 Dragboard 的拖放功能可以实现此功能。

JavaFX 拖板:

During the drag-and-drop gesture, various types of data can be transferred such as text, images, URLs, files, bytes, and strings.

The javafx.scene.input.DragEvent class is the basic class used to implement the drag-and-drop gesture. For more information on particular methods and other classes in the javafx.scene.input package, see the API documentation.

更多内容,您可以阅读本教程:Drag-and-Drop Feature in JavaFX Applications

Dragboard JavaFX 代码示例:HelloDragAndDrop.java

package hellodraganddrop;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.*;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Stage;

/**
 * Demonstrates a drag-and-drop feature.
 */
public class HelloDragAndDrop extends Application {

    @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);
        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();
    }

    public static void main(String[] args) {
        Application.launch(args);
    }
}

其实Java有一些复制文件的方法:4 Ways to Copy File in Java

资源链接:

  1. How do I properly handle file copy/cut & paste in javafx?

关于scala - JavaFX/ScalaFX 和剪贴板 : Cannot copy files?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37639902/

相关文章:

scala - 如何将完全格式化的 SQL 与 Spark 结构化流结合使用

Scala 递归函数

JavaFx 和内存消耗

JavaFX ImageView 内存泄漏

JavaFX JDK 9.0.4 ListView celFactory 添加空单元格

Scala "a"+ _.toString 的行为不像 "a".+(_.toString)

scala - 过滤案例类对象属性的最佳实践

php - 在ubuntu服务器5.3.6-13ubuntu3.6上安装xdebug 2.1.4

linux - 在ubuntu上安装node-sspi时出现错误

linux - 在 Oracle VM Virtual Box : Failed to open the optical disk file 上安装 Ubuntu