Creating a Clipboard History Application in Java with Swing

Sylvain Saurel
Jul 6 · 4 min read

In the IT world, a clipboard is a feature that allows you to store data that you want to duplicate or move. For this purpose, a zone of the computer’s volatile memory is used. Generally, the default clipboard can only contain one entry at a time. This is the last one copied. To solve this problem, we are going to see how to create a clipboard storing the history of the copied entries. This allows you to copy old entries again if necessary.

For creating a Clipboard History Application, we are going to use Java with Swing API for the User Interface. You can also watch this tutoriel on YouTube :

Creating a Clipboard Listener

First step is to create a Clipboard Listener. It will let our application to listen data copied on the underlying system via CTRL+C. For that, we create a ClipboardListener class which will extend the Thread class because our class will need to run in background for listening clipboard activity. In standard, Java offers a Clipboard API with a ClipboardOwner interface we need to implement.

In the lostOwnership method we implement, we start by wait during 200 milliseconds by calling the sleep method. Then we get contents from the clipboard instance passed in parameter of the method. Content got from the clipboard is represented by a Transferable object. We pass this Transferable instance inside a processContents method. In the processContents method, we get the String data contained in the Transferable instance. So, our Clipboard History Application will only offer support for String data.

Once we got the String data, we will need to inform the User Interface part of the value copied. To limit coupling between our Clipboard Listener and the UI part, we define a EntryListener interface with a onCopy method. The EntryListener interface will be implemented by our main class in the UI part. So, in the processContents method, we don’t forget to call the onCopy method of the EntryListener instance with the String data in parameter. At the end of the lostOwnership method, we regain ownership by calling the setContents method of the Clipboard instance.

Last step for the ClipboardListener class is to define its run method. First, we call getContents on the Clipboard instance. Then, we call regainOwnership on the clipboard and Transferable object got previously. Finally, we put in an infinite loop to keep listening the activity on the clipboard while the application is running.

This gives us the following complete code for the ClipboardListener:

Creating the User Interface

For creating the User Interface of our Clipboard History Application, we are going to use Swing API. Our User Interface will have basically two main components :

  • A List to display the entries copied in the clipboard
  • A Button letting users to copy a specific entry of our list

Besides, our ClipboardHistory class will implement the EntryListener interface. In the onCopy method implemented, we will add the String passed in parameter in our List of entries. With the Swing API, a List is represented by the JList class. We will need also a DefaultListModel for managing items in the JList. Then, we access the ListSelectedModel associated to our JList instance for setting a single selection mode.

After that, we add an ActionListener implementation to our copy Button. In this implementation, we get the selected value by calling the getSelectedValue method of our JList instance. Then we get the index of the selection by calling the getSelectedIndex method of the JList instance. Like the user chooses to copy an entry from our Clipboard History Application, we remove it from the list by calling the remove method of the DefaultListModel instance with the index passed in parameter. Finally, we call a copyToClipboard method to copy the data in the clipboard via the Clipboard API of Java. Obviously, it will be put the copied data at the top of the entries’ list of our Application.

It gives us the following complete code for the ClipboardHistory class:

Our Clipboard History Application in Action!

Best part of the tutorial is there. We put our Clipboard History Application in Action! Once the Application is launched, we copy some data and the result is displayed in the following image:

Clipboard History Application in Action!

That’s all for that tutorial :).

If you have some questions concerning the Clipboard History Application, feel free to use the comments.

Sylvain Saurel

Written by

Entrepreneur / Developer / Blogger / Author. In Bitcoin We Trust: https://www.inbitcoinwetrust.net

The Startup

Medium's largest active publication, followed by +538K people. Follow to join our community.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade