如何使用系统剪贴板从java中粘贴? [英] How would I make a paste from java using the system clipboard?

查看:471
本文介绍了如何使用系统剪贴板从java中粘贴?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在java中从系统剪贴板中粘贴。我该怎么做?

I want to make a paste from the system clipboard in java. How would I do this?

推荐答案

虽然机器人类可以工作,但它不像直接使用系统剪贴板那样优雅,比如这个:

While the robot class would work, it's not as elegant as using the system clipboard directly, like this:

private void onPaste(){
    Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
    Transferable t = c.getContents(this);
    if (t == null)
        return;
    try {
        jtxtfield.setText((String) t.getTransferData(DataFlavor.stringFlavor));
    } catch (Exception e){
        e.printStackTrace();
    }//try
}//onPaste

这篇关于如何使用系统剪贴板从java中粘贴?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆