Java Simple implementation copy paste cut feature code sharing _java

Source: Internet
Author: User
Tags event listener

Nonsense not to say, directly on the code, the small friends carefully read the note.

Copy Code code as follows:

/* Simple copy-cut-paste feature
Operation:
Copy test: Input text Select text, click Copy, then place the cursor on the right textarea, click Paste
Cut Test: Enter text select text, then place cursor on right textarea, 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; Upper and lower
Private JLabel title;
Private JTextArea edit,showmsg;
Private JButton copy,paste,cut;
Clipboard clipboard;//Gets the system Clipboard.

Public Demo ()
{
This.init ();
}
Interface initialization
public void Init ()
{
JF = new JFrame ("Copy and paste");
P1 = new JPanel (); Store title
P2 = new JPanel (); Store JTextArea showmsg
P3 = new JPanel (); Store button
title = new JLabel ("Copy and Paste 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 ("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 Event Listener 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 transferable that can transmit a 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); Deletes the selected text from the 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 ();
}
}

Code is very simple, easy to use, small partners have better ideas, please be sure to tell me.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.