Without further ado, let’s go straight to the code. Friends, please read the comments carefully.
Copy the code code as follows:
/*Simple copy, cut and paste function operations:
Copy test: Enter text, select text, click Copy, then place the cursor on the TextArea on the right, click Paste. Cut test: Enter text, select text, then place the cursor on the TextArea on the right, click Cut.
*/
import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.*;
import java.awt.event.*;
public class Demo implements ActionListener
{
private JFrame jf;
private JPanel p1, p2, p3; //top, middle and bottom
private JLabel title;
private JTextArea edit,showMsg;
private JButton copy,paste,cut;
Clipboard clipboard;//Get the system clipboard.
publicDemo()
{
this.init();
}
//interface initialization
public void init()
{
jf = new JFrame("Copy and Paste");
p1 = new JPanel(); //Storage title
p2 = new JPanel();//Storage JTextArea showMsg
p3 = new JPanel(); //Storage button
title = new JLabel("Copy, Paste and Cut Demo");
edit = new JTextArea("Please enter content",15,25);
edit.setLineWrap(true);
showMsg = new JTextArea(15,25);
showMsg.setLineWrap(true);
showMsg.setEnabled(false);
copy = new JButton("Copy");
paste = new JButton("Paste");
cut = new JButton("cut");
clipboard = jf.getToolkit().getSystemClipboard();
p1.setLayout(new FlowLayout());
p1.setSize(599,30);
p1.add(title);
p2.setLayout(new FlowLayout());
p2.setBackground(Color.gray);
p2.add(edit);
p2.add(showMsg);
p3.setLayout(new FlowLayout());
p3.add(copy);
p3.add(paste);
p3.add(cut);
//Add event listening mechanism
copy.addActionListener(this);
paste.addActionListener(this);
cut.addActionListener(this);
// this.copyStr(copy);
jf.add(p1, BorderLayout.NORTH);
jf.add(p2, BorderLayout.CENTER);
jf.add(p3, BorderLayout.SOUTH);
jf.setLocation(400,200);
jf.setSize(600,450);
jf.setResizable(false);
jf.setVisible(true);
}
//event handling
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == copy)
{
String tempText = edit.getSelectedText(); //Drag the mouse to select text
//Create a Transferable that can transfer the specified String.
StringSelection editText =
new StringSelection(tempText);
/**
Sets the current contents of the clipboard to the specified transferable object,
And registers the specified clipboard owner as the owner of the new content.
*/
clipboard.setContents(editText,null);
}else if(e.getSource() == cut)
{
String tempText = edit.getSelectedText();
StringSelection editText =
new StringSelection(tempText);
clipboard.setContents(editText,null);
int start= edit.getSelectionStart();
int end = edit.getSelectionEnd();
showMsg.replaceRange("",start,end); //Delete the selected text from Text1.
}else if(e.getSource() == paste)
{
Transferable contents = clipboard.getContents(this);
DataFlavor flavor= DataFlavor.stringFlavor;
if( contents.isDataFlavorSupported(flavor))
{
try
{
String str;
str = (String)contents.getTransferData(flavor);
showMsg.append(str);
}catch(Exception ex)
{
ex.printStackTrace();
}
}
}
}
public static void main(String[] args)
{
new Demo();
}
}
The code is very simple and easy to use. If you guys have better ideas, please be sure to tell me.