java copy paste from clipboard

Solutions on MaxInterview for java copy paste from clipboard by the best coders in the world

showing results for - "java copy paste from clipboard"
Axel
25 Nov 2019
1import java.awt.datatransfer.StringSelection;
2import java.awt.Toolkit;
3import java.awt.datatransfer.Clipboard;
4
5
6String myString = "This text will be copied into clipboard";
7StringSelection stringSelection = new StringSelection(myString);
8Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
9clipboard.setContents(stringSelection, null);
Leelou
30 Feb 2016
1
2import java.awt.Toolkit;
3import java.awt.datatransfer.Clipboard;
4import java.awt.datatransfer.DataFlavor;
5import java.awt.datatransfer.StringSelection;
6import java.awt.datatransfer.UnsupportedFlavorException;
7import java.io.IOException;
8
9public class Main {
10  public static void main(String[] args) {
11  }
12
13  private void copy() {
14    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
15    String text = "value";
16    StringSelection selection = new StringSelection(text);
17    clipboard.setContents(selection, null);
18  }
19
20  private void paste() {
21    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
22    DataFlavor flavor = DataFlavor.stringFlavor;
23    if (clipboard.isDataFlavorAvailable(flavor)) {
24      try {
25        String text = (String) clipboard.getData(flavor);
26        System.out.println(text);
27      } catch (UnsupportedFlavorException e) {
28        System.out.println(e);
29      } catch (IOException e) {
30        System.out.println(e);
31      }
32    }
33  }
34
35}
36