Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Java
Expertise: Advanced
Jun 29, 1999



Building the Right Environment to Support AI, Machine Learning and Deep Learning

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.setSize(400, 300);
	}  //  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.

Brett Spell
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date