How would I make a paste from java using the system clipboard?

前端 未结 6 1938
我寻月下人不归
我寻月下人不归 2021-01-02 21:35

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

6条回答
  •  情歌与酒
    2021-01-02 22:12

    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
    

提交回复
热议问题