9

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

2
  • 1
    Did you look at java.awt.Toolkit.getSystemClipboard() and the Clipboard class? What exactly is your question?
    – EboMike
    Jul 9, 2011 at 0:42
  • 1
    yeah I have looked it up on many sites but they return it as a string but I want to simulate a control v
    – Globmont
    Jul 9, 2011 at 0:50

6 Answers 6

17

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
1
  • Where does the "jtxtfield" object comes from?
    – Kyon Perez
    Mar 12, 2022 at 22:24
6

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)
{

}
0
5

Try this

public static void type(String characters) {
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
StringSelection stringSelection = new StringSelection( characters );
clipboard.setContents(stringSelection, instance);
//control+V is for pasting...
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
}
2

You could also try using the Clipboard class.

1

You have to use Java graphics library, eg. take a look at http://download.oracle.com/javase/1,5.0/docs/api/java/awt/datatransfer/Clipboard.html

1
  • 1
    Thanks for that but how would I actually paste?
    – Globmont
    Jul 9, 2011 at 0:46
0

You can use the Clipboard class as follows to achieve the paste:

public static void getClipboardContents() {
        String result = "";
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        //odd: the Object param of getContents is not currently used
        Transferable contents = clipboard.getContents(null);
        boolean hasTransferableText =
          (contents != null) &&
          contents.isDataFlavorSupported(DataFlavor.stringFlavor)
        ;
        if (hasTransferableText) {
    

  try {
            result = (String)contents.getTransferData(DataFlavor.stringFlavor);
            System.out.print(result);
          }
          catch (UnsupportedFlavorException | IOException ex){
            System.out.println(ex);
            ex.printStackTrace();
          }
        }
      }

The content from the system clipboard is found in the result string variable. Solution coming from: http://www.javapractices.com/topic/TopicAction.do?Id=82

2
  • Why do you initialize result? You won't use it to make a decision, so why? String result = "";
    – Kyon Perez
    Mar 12, 2022 at 22:29
  • Where does the actual "paste" occurs? Because nothing happens.
    – Kyon Perez
    Mar 12, 2022 at 22:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.