JSP clipboard copy and paste program

Source: Internet
Author: User
Tags error handling

Import java.awt.*;
Import java.awt.event.*;
Import java.awt.datatransfer.*;
Import javax.swing.*;

Clipboard Demo

public class Clipboarddemo extends JFrame implements clipboardowner{
Clipboard Clipboard; Clip Board
JTextArea jtacopyto=new JTextArea (5,10); Text boxes for copying
JTextArea jtapaste=new JTextArea (5,10); text box for pasting

Public Clipboarddemo () {
Super ("Copy/paste program using clipboard"); Calling the parent class constructor

Clipboard=toolkit.getdefaulttoolkit (). Getsystemclipboard (); Get system Clipboard

JButton btcopy=new JButton ("copy"); Copy button
JButton btpaste=new JButton ("paste"); Paste button
Jtacopyto.setlinewrap (TRUE); Set line Wrapping
Jtapaste.setlinewrap (TRUE);
Jtacopyto.setborder (Borderfactory.createtitledborder ("Copy to System Clipboard")); Set boundaries
Jtapaste.setborder (Borderfactory.createtitledborder ("Paste from the system shear plate");

Container Container=getcontentpane (); Get the container
JToolBar toolbar=new JToolBar (); Instantiating toolbars
Toolbar.add (btcopy); Add toolbar buttons
Toolbar.add (Btpaste);
Btcopy.addactionlistener (New Copylistener ()); Button Event handling
Btpaste.addactionlistener (New Pastelistener ());
Box box=new box (Boxlayout.x_axis); Instantiate box
Box.add (Jtacopyto); Add text boxes to box
Box.add (Jtapaste);
Container.add (Toolbar,borderlayout.north); Add toolbars to Containers
Container.add (Box,borderlayout.center); Add box to Container

SetSize (320,180); Set Window size
SetVisible (TRUE); Set window to Visual
Setdefaultcloseoperation (Jframe.exit_on_close); Exit Program when window closes
}

Class Copylistener implements ActionListener {//Copy data processing
public void actionperformed (ActionEvent event) {
StringSelection contents=new stringselection (Jtacopyto.gettext ()); Instantiating StringSelection objects with copy text box text
Clipboard.setcontents (contents, clipboarddemo.this); To set the system Clipboard contents
}
}

Class Pastelistener implements ActionListener {//Paste data processing
public void actionperformed (ActionEvent event) {
Transferable contents=clipboard.getcontents (this); Get Clipboard contents
if (Contents!=null && contents.isdataflavorsupported (dataflavor.stringflavor)) {//To determine whether the content is null or not, is a string
try{
String string= (String) contents.gettransferdata (Dataflavor.stringflavor); Converting content to Strings
Jtapaste.append (string); Insert string to paste text box
}catch (Exception ex) {
Ex.printstacktrace (); Error handling
}
}
}
}

public void Lostownership (Clipboard clip,transferable transferable) {//implement methods in the Clipboardowner interface
}

public static void Main (string[] args) {
New Clipboarddemo ();
}
}

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.