Java simple copy paste cut function code sharing

  • 2020-04-01 03:33:57
  • OfStack

Without further ado, go straight to the code and look at the comments.


/* Simple copy shear paste function
  Operation:   
   Copy the test : Enter text to select text, click copy, and place the cursor on the right TextArea, Click paste
   Cut test: enter text to select text, then place the cursor on the right TextArea, Click on the shear   
*/
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, bottom
 private JLabel title;
 private JTextArea edit,showMsg;
 private JButton copy,paste,cut;
 Clipboard clipboard;//Gets the system clipboard. < br / >  
 public Demo()
 {
  this.init();
 }
 //Interface initialization
 public void init()
 {
  jf = new JFrame(" Copy and paste ");
  p1 = new JPanel();   //Store the title
  p2 = new JPanel(); //Store JTextArea showMsg
  p3 = new JPanel();  //Hold the button < br / >   title = new JLabel(" Copy paste cut demo ");
  edit = new JTextArea(" Please enter the 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(" shear ");
  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 an 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
   //Creates a spring that can transfer a specified String. < br / >    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. < br / >   }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 simple and easy to use. If you have a better idea, please tell me.


Related articles: