Cut and Copy Text to Clipboard

Java 1.1 introduced the java.awt.datatransfer package and its associated classes, which provide the foundation for capabilities such as cut-and-paste. On the surface, the API may appear complex, but this simple application shows how easily you can write text information to the clipboard, as would be done for a cut or copy operation.

Storing information in the clipboard can be broken down into three simple steps: get a java.awt.Toolkit reference, use the Toolkit reference to get a reference to an instance of java.awt.datatransfer.Clipboard, and call a method in the Clipboard instance to store the data. These steps are illustrated in the actionPerformed() method of this sample code:

 import java.awt.*;import java.awt.datatransfer.*;import java.awt.event.*;public class ClipTest extends Frame implements				ActionListener, ClipboardOwner {	TextField tf = new TextField(20);	public static void main(String[] args) {		ClipTest ct = new ClipTest();		ct.setLayout(new FlowLayout());		ct.tf.addActionListener(ct);		ct.add(ct.tf);		ct.setSize(400, 300);		ct.setVisible(true);	}  //  public static void main()	public void actionPerformed(ActionEvent event) {		String cliptext = tf.getText();		Toolkit tk = this.getToolkit();		Clipboard cb = tk.getSystemClipboard();		cb.setContents(new StringSelection(cliptext), this);	}  //  public void actionPerformed()	public void lostOwnership(Clipboard cb, Transferable tr) {};}  //  public class ClipTest extends Frame

The first two steps previously described are simple and straightforward. However, some clarification on the third step, represented by the call to the Clipboard’s setContents() method, may be helpful. The setContents() method requires two parameters, each of which is an instance of a class defined in the java.awt.datatransfer package. The first is an instance of Transferable, and the second is an instance of ClipboardOwner. For string operations like this one, the StringSelection class (which implements the Transferable interface) is provided as part of the java.awt.datatransfer package, and can be easily instantiated using the constructor shown which expects a single String instance. The second parameter, an instance of ClipboardOwner, is used to send a notification when the contents of the clipboard are eventually overwritten, specifically by calling the lostOwnership() method. In this case, no action needs to be taken, so the method is implemented but does not contain any code. If you compile and run this program, it will display a Frame containing a single TextField. Pressing Enter when the TextField has the input focus will cause the program to store the TextField’s String contents in the system clipboard. You can test it out by typing something in the TextField, pressing Enter, and then performing a paste operation in some other application. As you can see, using the java.awt.datatransfer package is simple, and using this sample code as a starting point, you can implement other, more complex cut, copy, and paste functionality.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS