|
Drag and Drop: New Data Transfer Capabilities in the JavaTM 2 Platform, Standard Edition (J2SETM), version 1.4(6) Getting the clipboard data is a little more involved, but not too difficult. There are basically three steps involved: - Get the reference to the data from the clipboard, the
Transferable object. - Find a flavor you can handle that it supports, where
DataFlavor.stringFlavor is for text strings. - Get the data for that flavor.
And, here's what that code looks like. There's also a bit of exception handling necessary as a UnsupportedFlavorException or IOException could be thrown. // Get data from clipboardTransferable clipData =clipboard.getContents(clipboard);// Make sure not emptyif (clipData != null) {// Check if it supports the desired flavorif (clipData.isDataFlavorSupported (DataFlavor.stringFlavor)) {// Get data String s = (String)(clipData.getTransferData(DataFlavor.stringFlavor));// Use data ...}}Clipboard Text ExampleAs shown in the following figure, the sample program that puts all these pieces together involves a text area and two buttons, Copy and Paste. When the Copy button is selected, the current selection within the text area is copied to the system clipboard. When the Paste button is selected, the current selection will be replaced with the contents of the system clipboard. If the flavor of data on the clipboard is not supported, the system beeps. Most of the code involves just the screen setup. There should be no new code as far as the clipboard Access. Java.sun.com/images/release_data_clip.jpg" width=300>
And, here's the complete source. import Java.awt.*;import Java.awt.event.*;import Java.awt.datatransfer.*;import Javax.swing.*;public class ClipboardExample {public static void main(String args[]) { JFrame frame = new JFrame("Copy/Paste"); Container contentPane = frame.getContentPane(); final Toolkit kit = Toolkit.getDefaultToolkit(); final Clipboard clipboard =kit.getSystemClipboard(); final JTextArea jt = new JTextArea(); JScrollPane pane = new JScrollPane(jt); contentPane.add(pane, BorderLayout.CENTER);JPanel bottom = new JPanel(); JButton copy = new JButton("Copy"); bottom.add(copy); ActionListener copyListener = new ActionListener() {public void actionPerformed(ActionEvent e) {String selection = jt.getSelectedText();StringSelection data = newStringSelection(selection);clipboard.setContents(data, data);} }; copy.addActionListener(copyListener); JButton paste = new JButton("Paste"); bottom.add(paste); ActionListener pasteListener = new ActionListener() {public void actionPerformed(ActionEventactionEvent) {Transferable clipData =clipboard.getContents(clipboard);if (clipData != null) { try {if (clipData.isDataFlavorSupported( DataFlavor.stringFlavor)) {String s =(String)(clipData.getTransferData( DataFlavor.stringFlavor));jt.replaceSelection(s);} else {kit.beep();} } catch (Exception e) {System.err.println("Problems getting data:" + e); }}} }; paste.addActionListener(pasteListener); contentPane.add(bottom, BorderLayout.SOUTH); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300, 300); frame.show();}}
|