This article illustrates the implementation of Java using Clipboard Exchange program data. In a graphical system, the system Clipboard is very important, and it is hard to imagine what a graphical operating system without the Clipboard function would be used for. This example realizes the data exchange between the Java program and the clipboard of the system in which it resides. When the Paste button is clicked, the Java program obtains the data from the system Clipboard and displays it in a JTextArea component, and when the Copy button is clicked, the selected text in the text area is routed to the system Clipboard.
First you must obtain an instance reference to the system Clipboard, and the Getsystemclipboard () method is provided in the Java.awt.Toolkit class to return a clipboard instance, and because Toolkit class provides a static method Getdefaulttoolkit () Returns a toolkit instance, and therefore does not need to have a new Toolkit object, which implements the following code:
Clipboard cb = Toolkit.getdefaulttoolkit (). Getsystemclipboard ();
The Clipboard class here provides a getcontents () and Setcontents () method for data interchange.
Transferable getcontents (Object requester);
Void setcontents (transferable contents, clipboardowner owner);
The getcontents () method here obtains a transferable object from the system Clipboard, and the parameter requester represents the data requester, which is usually done with this, indicating that the requested data is an instance object of this class. If the required data is text, you can call the transferable object's Gettransferdata (Dataflavor.stringflavor) and implement the following code:
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 preceding statement represents the selected text.
From the above analysis, in fact the system Clipboard is a collection of transferable objects, the program and the system Clipboard data exchange is transferable object transfer. Program code:
1. Create a new project named Jclipdemo.
2. Create a new application named Jclipdemo; the main window name is mainframe and the title is Jclipdemo.
3. Add a JTextArea component, two JButton, and a JPanel component to the design window of the mainframe class, and two JButton components on 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 ();
Create a new component
private JTextArea jTextArea1 = newer 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 for the mainframe class Jbinit (), complete the initial property settings for each component, and add event listeners to the button components as follows:
private void Jbinit () throws Exception {//seticonimage ().
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. Writes 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 () ); 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); Gets a 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 ()); Inserts the text at the cursor location in the JTextArea
component
}catch (Exception err) {
err.printstacktrace ();
}
}