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
You could use the robot class like this
try
{
Robot r = new Robot();
r.keyPress(KeyEvent.VK_CONTROL);
r.keyPress(KeyEvent.VK_V);
r.keyRelease(KeyEvent.VK_CONTROL);
r.keyRelease(KeyEvent.VK_V);
}
catch(Exception e)
{
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With