The following example is a simple demonstration of cut, copy, and paste String data type with a JTextArea. One thing you will notice is that the keyboard sequences normally used to cut, copy and paste also work. But if you look at any JTextField or JTextArea in any other program, you will find that these also support keyboard sequences clipboard automatically. this example simply adds programmatic control of the clipboard, and You can use these techniques if you want to capture text from the clipboard into something else than a JTextComponent.
Creating and added menu and JTextArea should by now seem a prosaic activity. What is different is the creation of the field Cboard Clipboard, which is done through Toolkit.
All actions take place in the listeners. The Copy and listeners Cut are the same except for the last line Cut, erasing the line has been copied. The two special lines are creating an object StringSelection the String and the call to setContents () with this StringSelection. All that is there is to put a String to the clipboard.
Paste, the data is removed from the clipboard using getContents (). What comes back is a fairly anonymous Transferable object, and it is uncertain actually it contains. One way to know is to call getTransferFlavors (), which returns an array of DataFlavor objects indicating that flavors are supported by this particular object. It You can also ask directly isDataFlavorSupported () passing the taste in which we are interested. Here, however, a bold strategy is used: getTransferData () is called assuming content supports the flavor of String, and if it does the problem is solved in the exception handler.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
public class ClipboardJavaExample extends JFrame
{
JMenuBar MenuBar = new JMenuBar();
JMenu File = new JMenu("File");
JMenu edit = new JMenu("Edit");
JMenuItem cut = new JMenuItem("Cut"),
copy = new JMenuItem("Copy"),
paste = new JMenuItem("Paste");
JTextArea TextArea = new JTextArea(20, 20);
Clipboard Cboard = getToolkit().getSystemClipboard();
public ClipboardJavaExample()
{
cut.addActionListener(new Cut());
copy.addActionListener(new Copy());
paste.addActionListener(new Paste());
edit.add(cut);
edit.add(copy);
edit.add(paste);
MenuBar.add(File);
MenuBar.add(edit);
setJMenuBar(MenuBar);
getContentPane().add(TextArea);
}
class Copy implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String selection = TextArea.getSelectedText();
if (selection == null)
return;
StringSelection clipString =new StringSelection(selection);
Cboard.setContents(clipString,clipString);
}
}
class Cut implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String selection = TextArea.getSelectedText();
if (selection == null)
return;
StringSelection clipString = new StringSelection(selection);
Cboard.setContents(clipString, clipString);
TextArea.replaceRange("", TextArea.getSelectionStart(), TextArea.getSelectionEnd());
}
}
class Paste implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Transferable clipData = Cboard.getContents(ClipboardJavaExample.this);
try
{
String clipString = (String)clipData.getTransferData(DataFlavor.stringFlavor);
TextArea.replaceRange(clipString,TextArea.getSelectionStart(),TextArea.getSelectionEnd());
} catch(Exception ex)
{
System.err.println("Not Working");
}
}
}
public static void main(String[] args)
{
JFrame frame = new ClipboardJavaExample();
frame.setTitle("Clipboard in Java Swing Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(320, 200);
frame.setVisible(true);
}
}