Import java.awt.*;
Import java.awt.event.*;
Import java.awt.datatransfer.*;
Import javax.swing.*;
Clipboard Demo
public class Clipboarddemo extends JFrame implements clipboardowner{
Clipboard Clipboard; Clip Board
JTextArea jtacopyto=new JTextArea (5,10); Text boxes for copying
JTextArea jtapaste=new JTextArea (5,10); text box for pasting
Public Clipboarddemo () {
Super ("Copy/paste program using clipboard"); Calling the parent class constructor
Clipboard=toolkit.getdefaulttoolkit (). Getsystemclipboard (); Get system Clipboard
JButton btcopy=new JButton ("copy"); Copy button
JButton btpaste=new JButton ("paste"); Paste button
Jtacopyto.setlinewrap (TRUE); Set line Wrapping
Jtapaste.setlinewrap (TRUE);
Jtacopyto.setborder (Borderfactory.createtitledborder ("Copy to System Clipboard")); Set boundaries
Jtapaste.setborder (Borderfactory.createtitledborder ("Paste from the system shear plate");
Container Container=getcontentpane (); Get the container
JToolBar toolbar=new JToolBar (); Instantiating toolbars
Toolbar.add (btcopy); Add toolbar buttons
Toolbar.add (Btpaste);
Btcopy.addactionlistener (New Copylistener ()); Button Event handling
Btpaste.addactionlistener (New Pastelistener ());
Box box=new box (Boxlayout.x_axis); Instantiate box
Box.add (Jtacopyto); Add text boxes to box
Box.add (Jtapaste);
Container.add (Toolbar,borderlayout.north); Add toolbars to Containers
Container.add (Box,borderlayout.center); Add box to Container
SetSize (320,180); Set Window size
SetVisible (TRUE); Set window to Visual
Setdefaultcloseoperation (Jframe.exit_on_close); Exit Program when window closes
}
Class Copylistener implements ActionListener {//Copy data processing
public void actionperformed (ActionEvent event) {
StringSelection contents=new stringselection (Jtacopyto.gettext ()); Instantiating StringSelection objects with copy text box text
Clipboard.setcontents (contents, clipboarddemo.this); To set the system Clipboard contents
}
}
Class Pastelistener implements ActionListener {//Paste data processing
public void actionperformed (ActionEvent event) {
Transferable contents=clipboard.getcontents (this); Get Clipboard contents
if (Contents!=null && contents.isdataflavorsupported (dataflavor.stringflavor)) {//To determine whether the content is null or not, is a string
try{
String string= (String) contents.gettransferdata (Dataflavor.stringflavor); Converting content to Strings
Jtapaste.append (string); Insert string to paste text box
}catch (Exception ex) {
Ex.printstacktrace (); Error handling
}
}
}
}
public void Lostownership (Clipboard clip,transferable transferable) {//implement methods in the Clipboardowner interface
}
public static void Main (string[] args) {
New Clipboarddemo ();
}
}