Baeldung

Java, Spring and Web Development tutorials

 

Copying Text to the Clipboard in Java
2025-07-07 03:14 UTC by Haidar Ali

1. Overview

In this tutorial, we’ll discuss how to copy text to the system clipboard in Java. These methods aren’t limited to GUI applications; they’ll also apply to console applications.

We’ll look at the AWT toolkit and learn how to copy-paste text in Java that works on all platforms.

2. The AWT Toolkit

AWT or Abstract Window Toolkit is a GUI API that we can use to develop cross-platform graphical applications using Java. Fundamentally, it’s a wrapper over the native OS GUI. Therefore, it can access resources such as native user interfaces, including the native clipboard API.

We aim to copy text to the clipboard in all types of Java applications, including console programs. Even though we’re not specifically interested in the GUI, AWT lets us copy text to the clipboard without running the underlying GUI engine.

2.1. Copying to Clipboard

First, let’s write a method that copies text to the clipboard:

public static void copyToClipboard(String text) {
    Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
    StringSelection data = new StringSelection(text);
    cb.setContents(data, null);
}

The getDefaultToolkit() method returns a toolkit instance for the current platform. Similarly, the getSystemClipboard() method creates an instance of the platform shared clipboard. StringSelection makes it easier to copy plain text data to the system clipboard.

The setContents() method expects the text to be copied and the ClipboardOwner. ClipboardOwner lets us track the ownership of data that we’ve copied to the system clipboard. Therefore, if any other part of our program or another process copies data to the system clipboard, we’ll be notified through a callback. Inside the callback, we can carry out cleanup and other operations, such as clearing sensitive data or logging statistics.

In our code, we pass null as the ClipboardOwner because we don’t need to be notified about losing ownership of the clipboard. Generally, we don’t need ownership for simple copy-paste operations.

2.2. Retrieving from Clipboard

Similarly, we can retrieve data from the clipboard as well:

public static String copyFromClipboard() throws UnsupportedFlavorException, IOException {
    Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
    Transferable transferable = cb.getContents(null);
    if (transferable.isDataFlavorSupported(DataFlavor.stringFlavor)) {
        String data = (String) transferable.getTransferData(DataFlavor.stringFlavor);
        return data;
    }
    System.out.println("Couldn't get data from the clipboard");
    return null;
}

Again, we create an instance of the system Clipboard. Then, we retrieve the raw string data from the clipboard into a Transferable. Next, we check if the retrieved data can be used as a standard String. We do so by checking it against theĀ stringFlavor of the DataFlavor class. DataFlavor provides metadata about the data in the clipboard. For instance, if we’re expecting image data, we’d use DataFlavor.imageFlavor. Additionally, there are other flavors we can check the data against.

In the code, we’re expecting the data to be a String. In that case, we simply cast the retrieved data to String because getTransferData() returns an Object that we must cast into an appropriate type. Finally, we return the data.

Additionally, we must handle exceptions when retrieving data from the clipboard. If we retrieve the data directly and it fails, the entire program might crash. Therefore, we should either use if guards or handle the appropriate exceptions.

3. Conclusion

In this article, we’ve learned how to copy text to the clipboard in Java that works across Windows, macOS, and Linux. As always, the code used in this article can be found over on GitHub.

The post Copying Text to the Clipboard in Java first appeared on Baeldung.
       

 

Content mobilized by FeedBlitz RSS Services, the premium FeedBurner alternative.