A System Clipboard is a part of Swing and AWT API that implements a mechanism to transfer data using cut/copy/paste operations.
With the flowing source code first we set a text into System Clipboard and then register a listener for printing change of the System Clipboard content.
package saeedswing;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.FlavorEvent;
import java.awt.datatransfer.FlavorListener;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
/**
*
* @author Saeed Zarinfam
*/
public class NewMain {
public static void main(String[] args) {
String someText = "Saeed";
StringSelection ss = new StringSelection(someText);
Clipboard systemClipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
systemClipboard.setContents(ss, null);
systemClipboard.addFlavorListener(new FlavorListener() {
public void flavorsChanged(FlavorEvent e) {
try {
Clipboard clipboard = (Clipboard) e.getSource();
System.out.println("flavorsChanged : "
+ clipboard.getContents(null).getTransferData(DataFlavor.stringFlavor));
} catch (UnsupportedFlavorException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
JFrame jFrame = new JFrame();
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jFrame.setVisible(true);
}
}
have a nice time.