Java/Swing : ownership of the system clipboard

标签 java image swing awt clipboard

我正在编写一个小的 Java 程序,它应该运行一个外部程序将图像复制到系统剪贴板(即 Windows 7“截图工具”),等待它完成,将图像从剪贴板保存到磁盘并将 URL(可以从中访问图像)复制到剪贴板。简而言之,它应该:

  1. 运行外部工具并等待
  2. 从剪贴板复制一张图片
  3. 复制一个字符串到剪贴板

这个,我的程序是完全可以做到的。但是,我想使用 Swing/AWT 来呈现用户界面。我使用的是系统托盘图标,但为了简单起见,它也可以是框架中的 JButton。单击按钮时,应执行上述过程。第一次这样做时,它会正常工作。图像被复制、粘贴到磁盘,字符串被复制到剪贴板。然后,第二次单击该按钮时,就好像我的程序没有意识到剪贴板已经更新,因为它仍然看到它自己的字符串,这是第一次发生的。只有在这之后,我的剪贴板处理类才会失去所有权,实际上,该过程的每一次尝试都会失败。

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;

public class Main {
    private static BufferedImage image; //the image from clipboard to be saved

    public static void main(String[] args) throws InterruptedException, IOException {
        new GUI();
    }

    public static void run(String filename) throws IOException, InterruptedException {
        CBHandler cbh = new CBHandler();

        //run tool, tool will copy an image to system clipboard
        Process p = Runtime.getRuntime().exec("C:\\Windows\\system32\\SnippingTool.exe");
        p.waitFor();

        //copy image from clipboard
        image = cbh.getClipboard();
        if(image == null) {
            System.out.println("No image found in clipboard.");
            return;
        }

        //save image to disk...

        //copy file link to clipboard
        String link = "http://somedomain.com/" + filename;
        cbh.setClipboard(link);
    }
}

class CBHandler implements ClipboardOwner {
    public BufferedImage getClipboard() {
        Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);

        try {
            if(t.isDataFlavorSupported(DataFlavor.imageFlavor))
                return (BufferedImage) t.getTransferData(DataFlavor.imageFlavor);
        }
        catch(Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public void setClipboard(String str) {
        StringSelection strsel = new StringSelection(str);
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(strsel, this);
    }

    @Override
    public void lostOwnership(Clipboard arg0, Transferable arg1) {
        System.out.println("Lost ownership!");
    }
}

class GUI extends JFrame {
    public GUI() {
        JButton button = new JButton("Run");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                try {
                    Main.run("saveFile.png");
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        add(button);
        pack();
        setVisible(true);
    }
}

如果您尝试运行它,请注意在第二次运行时,lostOwnership 方法仅在尝试复制图像后被调用。我猜这是我的问题的根源,我不知道它为什么会发生,除了它只在由 Swing 事件触发时发生。任何解决此问题的帮助都将不胜感激。

最佳答案

一个猜测:您正在 AWT 事件分派(dispatch)线程(例如,直接从 ActionListener 或类似线程)上进行整个处理(调用其他进程)。

剪贴板更改消息也将由 EDT 上的 VM 处理……但仅在您完成按钮单击之后。

道德:不要在 EDT 上做长时间运行的事情(以及应该在事件队列中排队的事情) - 相反,为此启动一个新线程。

关于Java/Swing : ownership of the system clipboard,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7216074/

相关文章:

ios - 在 iOS 中将一个图像覆盖在另一个图像上

java - 在 JMX 中设置每用户或每组安全性

java - 使用 Collections.sort() 方法按字母顺序对对象进行排序

java - xsl 中枚举的值

android - 如何在 Android 的 ImageView 中显示 gif 图像?

javascript - 制作一个显示骰子图片的多个随机骰子滚筒。为什么我的代码不起作用?

java - 找不到符号 HibernateUtil

java - 如何处理 tablecellrenderer 中重复的列标题?

java - 在整个 JTable 组件上右键单击 MouseListener

java - 布局管理器可以生成多个 JPanel 吗?