Answers for "how to copy text to clipboard in java"

2

copy to clipboard java

import java.awt.datatransfer.StringSelection;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;


String myString = "This text will be copied into clipboard";
StringSelection stringSelection = new StringSelection(myString);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(stringSelection, null);
Posted by: Guest on January-09-2021
1

java copy paste from clipboard

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;

public class Main {
  public static void main(String[] args) {
  }

  private void copy() {
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    String text = "value";
    StringSelection selection = new StringSelection(text);
    clipboard.setContents(selection, null);
  }

  private void paste() {
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    DataFlavor flavor = DataFlavor.stringFlavor;
    if (clipboard.isDataFlavorAvailable(flavor)) {
      try {
        String text = (String) clipboard.getData(flavor);
        System.out.println(text);
      } catch (UnsupportedFlavorException e) {
        System.out.println(e);
      } catch (IOException e) {
        System.out.println(e);
      }
    }
  }

}
Posted by: Guest on August-11-2021

Code answers related to "how to copy text to clipboard in java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language