This article describes the implementation method of Java using the clipboard to exchange data between programs. In graphical systems, the system clipboard is very important. It is difficult to imagine how a graphical operating system without a clipboard function would be used. This example realizes the data exchange between the Java program and the clipboard of the system. When the "Paste" button is clicked, the Java program obtains the data from the system clipboard and displays it in a JTextArea component; when the "Copy" button is clicked After that, the selected text in the text area will be transferred to the system clipboard.
First, you must get an instance reference to the system clipboard. The java.awt.Toolkit class provides the getSystemClipboard() method to return a Clipboard instance; and since the Toolkit class provides the static method getDefaultToolkit() to return a Toolkit instance, there is no need to create a new one. Toolkit object, the specific implementation code is as follows:
Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
Here, the Clipboard class provides getContents() and setContents() methods to implement data exchange.
Transferable getContents(Object requester);Void setContents(Transferable contents, ClipboardOwner owner);
The getContents() method here obtains a Transferable object from the system clipboard. The parameter requester represents the data applicant. Generally, this is enough, indicating that the request for data is an instance object of this class. If the required data is text, you can call getTransferData(DataFlavor.stringFlavor) of the Transferable object to obtain it. The implementation code is as follows:
Transferable tr = cb.getContents(this);String s = (String) tr.getTransferData(DataFlavor.stringFlavor);
The setContents() method transfers data from the program to the system clipboard. The parameter contents represents the data, and the parameter owner represents the owner of the clipboard.
StringSelection ss = new StringSelection(this.jTextArea1.getText());cb.setContents(ss,ss);
The StringSelection class in the above statement represents the selected text.
From the above analysis, in fact, the system clipboard stores a collection of Transferable objects, and the data exchange between the program and the system clipboard is the transfer of Transferable objects. Program code:
1. Create a new Project and name it JClipDemo.
2. Create a new Application and name it JClipDemo; name the main window MainFrame and title it JClipDemo.
3. Add a JTextArea component, two JButtons, and a JPanel component to the design window of the MainFrame class, and place the two JButton components on the JPanel component. Add new property Clipboard cb. The specific code is as follows:
public class MainFrame extends JFrame {private JPanel contentPane;private BorderLayout borderLayout1 = new BorderLayout();//Create a new component private JTextArea jTextArea1 = new JTextArea();private JPanel jPanel1 = new JPanel();private JButton jButton1 = new JButton( );private JButton jButton2 = new JButton();//Clipboard instanceClipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();……}
4. Write the initialization method jbInit() of the MainFrame class, complete the initial property settings of each component, and add an event listener for the button component. The specific code is as follows:
private void jbInit() throws Exception {//setIconImage(Toolkit.getDefaultToolkit().createImage(MainFrame.class.getResource("[Your Icon]")));contentPane = (JPanel) this.getContentPane();contentPane.setLayout (borderLayout1);this.setSize(new Dimension(396, 203));this.setTitle("JClipboardDemo");jButton1.setFont(new java.awt.Font("Dialog", 0, 14));jButton1.setText("Copy");jButton1.addActionListener(new java. awt.event.ActionListener() { //Add event listener public void actionPerformed(ActionEvent e) {jButton1_actionPerformed(e);}});jButton2.setFont(new java.awt.Font("Dialog", 0, 14));jButton2.setText("Paste");jButton2.addActionListener(new java.awt .event.ActionListener() {//Add event listener public void actionPerformed(ActionEvent e) {jButton2_actionPerformed(e);}});contentPane.add(jTextArea1, BorderLayout.CENTER);contentPane.add(jPanel1, BorderLayout.SOUTH);jPanel1.add(jButton1, null);jPanel1.add(jButton2, null); }
5. Write the event handling method of the "Copy" button to send data to the system clipboard.
void jButton1_actionPerformed(ActionEvent e) {StringSelection ss = new StringSelection(this.jTextArea1.getText()); //Send the selected text to the system clipboard cb.setContents(ss,ss);}
6. Write the event handling method of the "Paste" button to obtain data from the system clipboard.
void jButton2_actionPerformed(ActionEvent e) {try{Transferable tr = cb.getContents(this); //Get a Transferable object from the system clipboard if (tr != null){String s = (String) tr.getTransferData(DataFlavor.stringFlavor ); //Get text data from Transferable object if (s!=null)this.jTextArea1.insert(s,this.jTextArea1.getCaretPosition()); //Insert text at the cursor position in the JTextArea component}}catch(Exception err){err.printStackTrace();} }