Java USES the clipboard to exchange data between programs

  • 2020-04-01 03:25:07
  • OfStack

This article illustrates an example of how Java USES the clipboard to exchange data between programs. In graphical systems, the system clipboard is so important that it's hard to imagine how a graphical operating system without clipboard functionality would work. This example realizes the data exchange between the Java program and the clipboard of the system where the program is located. 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, the selected text in the text area is passed to the system clipboard.

You must first 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 an Toolkit instance, there is no need to create a new Toolkit object. The implementation code is as follows:


Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();

Here Clipboard class provides getContents() and setContents() methods for data exchange.


Transferable getContents(Object requester);
Void setContents(Transferable contents, ClipboardOwner owner);

The getContents() method here gets an Transferable object from the system clipboard. The parameter requester represents the data applicant. If the requested data is text, the getTransferData(dataflavor.stringflavor) of the transferobject can be obtained by calling the transferobject's getTransferData(dataflavor.stringflavor). The implementation code is as follows:


Transferable tr = cb.getContents(this);
String s = (String) tr.getTransferData(DataFlavor.stringFlavor);

The setContents() method passes 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 is the 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 call it JClipDemo.
2. Create a new Application called JClipDemo; The main window name is called MainFrame and the title is 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 top of the JPanel component. Add a new property Clipboard cb. The specific code is as follows:


public class MainFrame extends JFrame {
private JPanel contentPane;
private BorderLayout borderLayout1 = new BorderLayout();
//Creating a new component
private JTextArea jTextArea1 = new JTextArea();
private JPanel jPanel1 = new JPanel();
private JButton jButton1 = new JButton();
private JButton jButton2 = new JButton();
//Clipboard instance
Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
 ... 
}

4. Write the initialization method jbInit() for the MainFrame class, complete the initial property setting for each component, and add an event listener for the button component, 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 an 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 an 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 an event handling method for the "Copy" button to send data to the system clipboard.


void jButton1_actionPerformed(ActionEvent e) {
StringSelection ss = new StringSelection(this.jTextArea1.getText()); //Sends the selected text to the system clipboard
cb.setContents(ss,ss);
}

6. Write the event handling method for the "Paste" button to get the data from the system clipboard.


void jButton2_actionPerformed(ActionEvent e) {
try{
Transferable tr = cb.getContents(this); //Get an Transferable object from the system clipboard
if (tr != null){
String s = (String) tr.getTransferData(DataFlavor.stringFlavor); //Get text data from the Transferable object
if (s!=null)
this.jTextArea1.insert(s,this.jTextArea1.getCaretPosition()); //Insert text where the cursor is in the JTextArea component
}
}catch(Exception err){
err.printStackTrace();
}
}

Related articles: