首页 > 解决方案 > JavaFX Clipboard 类不会将字符串放入系统剪贴板

问题描述

我正在使用在脚本标记中声明的一些 js 函数(工作正常),editor.html因为我需要为我的项目使用ace 编辑器。一切正常,我只需要在WebView. 除了它不允许我使用Clipboard该类从 webview 复制文本。它应该可以正常工作,它将字符串放在剪贴板实例中,但我无法粘贴它。这是代码:

package com.mightycoderx.javafx;

import com.sun.istack.internal.Nullable;
import javafx.concurrent.Worker;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.input.*;
import javafx.scene.layout.HBox;
import javafx.scene.web.WebView;
import netscape.javascript.JSObject;

import java.io.*;
import java.net.URL;
import java.util.ResourceBundle;

public class Controller implements Initializable
{
    @FXML
    private WebView editor;

    @FXML
    public HBox buttonBox;

    @FXML
    public Button btnCompile;

    @FXML
    private Button btnRun;

    @FXML
    public Button btnCompileAndRun;

    @FXML
    private TextArea txtConsole;

    @Override
    public void initialize(URL location, ResourceBundle resources)
    {
        File file = new File("Main.java");

        editor.getEngine().setJavaScriptEnabled(true);
        editor.getEngine().load(Controller.class.getResource("editor.html").toExternalForm().replace("file:/", "file:///"));

        editor.getEngine().getLoadWorker().stateProperty().addListener((observable, oldValue, newValue) -> {
            if (newValue == Worker.State.SUCCEEDED)
            {
                editor.getEngine().executeScript("initEditor()");
                buttonBox.setDisable(false);
            }
        });

        final KeyCombination copyCombo = new KeyCodeCombination(KeyCode.C, KeyCombination.CONTROL_DOWN);

        editor.addEventFilter(KeyEvent.KEY_PRESSED, e ->
        {
            if (copyCombo.match(e)) {
                onCopy();
            }
        });

        btnCompile.setOnAction(e ->
        {
            compile(file);
            btnRun.setDisable(false);
        });

        btnRun.setOnAction(e -> run(file));

        btnCompileAndRun.setOnAction(e ->
        {
            compile(file);
            run(file);
        });
    }

    private void onCopy()
    {
        String contentText = (String) editor.getEngine().executeScript("copySelection()");

        Clipboard clipboard = Clipboard.getSystemClipboard();
        ClipboardContent content = new ClipboardContent();
        content.putString(contentText);
        clipboard.setContent(content);

        System.out.println("Clipboard: " + clipboard.getString());
    }

    public void compile(File file)
    {
        try
        {
            file.createNewFile();
            FileWriter writer = new FileWriter(file.getPath());
            writer.write((String) editor.getEngine().executeScript("getValue()"));
            writer.close();

            Process pr = Runtime.getRuntime().exec("javac " + file.getPath());
            int exitCode = printProcessOutput(pr);
            if(exitCode == 0)
            {
                println("Compiled successfully!\n");
            }
        }
        catch (IOException | InterruptedException ioException)
        {
            ioException.printStackTrace();
        }
    }

    public void run(File file)
    {
        if(!txtConsole.getText().isEmpty())
        {
            txtConsole.clear();
        }

        try
        {
            Process pr =  Runtime.getRuntime().exec("java " + file.getPath().replace(".java", ""));
            int exitCode = printProcessOutput(pr);
            println("Process ended with exit code " + exitCode);
        }
        catch (IOException | InterruptedException ex)
        {
            ex.printStackTrace();
        }
    }

    public int printProcessOutput(Process pr) throws InterruptedException
    {
        try
        {
            BufferedReader stdInput = new BufferedReader(new InputStreamReader(pr.getInputStream()));
            BufferedReader stdError = new BufferedReader(new InputStreamReader(pr.getInputStream()));

            String s = null;
            while ((s = stdInput.readLine()) != null)
            {
                println(s);
            }

            s = null;
            while ((s = stdError.readLine()) != null)
            {
                println(s);
            }
        }
        catch (IOException ex)
        {
            ex.printStackTrace();
        }

        return pr.waitFor();
    }

    public void println(@Nullable String str)
    {
        if(str == null)
        {
            str = "";
        }
        txtConsole.setText(txtConsole.getText() + str + "\n");
    }
}

我搞砸了什么还是只是一个错误?因为如果我onCopy以其他方式使用,例如在窗口加载时复制硬编码字符串,它可以工作,但如果我在 ctrl+c 按下时使用 onCopy,它就不起作用。

标签: javajavafxclipboard

解决方案


基本上问题是ace编辑器试图与我的java函数一起复制并用空字符串覆盖剪贴板


推荐阅读