Java Clipboard Operations

Source: Internet
Author: User

Java 1.1 provides limited operational support for the system Clipboard (in Java.awt.datatransfer package). We can copy the string to the Clipboard, and we can paste the text from the Clipboard diagonally in the character. Of course, the Clipboard is designed to hold various types of data, and the data that exists on the Clipboard is run by the program to cut and paste into the program. Although the Clipboard currently supports only string data, the Java Clipboard API provides good scalability through the "featured" concept. When data is coming out of the clipboard, it has an associated set of features that can be modified (for example, a graphic can be represented as a string or an image) and we will notice that if special clipboard data supports this feature, we will be very interested.
The following program simply demonstrates the cutting, copying, and pasting of string data in textarea. What we will notice is that we need to work in the order of cutting, copying, and pasting. But if we see TextField or textarea in other programs, we find that they also automatically support the order in which the Clipboard is operated. The program simply adds programmatic control to the Clipboard, which can be used if we want to use it to capture text from the Clipboard to some non-text components.
 

: Cutandpaste.java//Using the Clipboard from Java 1.1 import java.awt.*;
Import java.awt.event.*;

Import java.awt.datatransfer.*;
  public class Cutandpaste extends Frame {MenuBar MB = new MenuBar ();
  Menu edit = new Menu ("edit");
  MenuItem cut = new MenuItem ("Cut"), copy = new MenuItem ("Copy"), paste = new MenuItem ("paste");
  TextArea Text = new TextArea (20,20);
  Clipboard CLIPBD = Gettoolkit (). Getsystemclipboard ();
    Public Cutandpaste () {Cut.addactionlistener (New cutl ());
    Copy.addactionlistener (New Copyl ());
    Paste.addactionlistener (new Pastel ());
    Edit.add (cut);
    Edit.add (copy);
    Edit.add (paste);
    Mb.add (edit);
    Setmenubar (MB);
  Add (text, borderlayout.center); Class Copyl implements ActionListener {public void actionperformed (ActionEvent e) {String selection = text
      . Getselectedtext ();
      StringSelection clipstring = new StringSelection (selection); Clipbd.setcontents (clipstring, clipstring);
    Class Cutl implements ActionListener {public void actionperformed (ActionEvent e) {String selectio
      n = text.getselectedtext ();
      StringSelection clipstring = new StringSelection (selection);
      Clipbd.setcontents (clipstring, clipstring);
    Text.replacerange ("", Text.getselectionstart (), Text.getselectionend ()); } class Pastel implements ActionListener {public void actionperformed (ActionEvent e) {transferable CLIPD
      ATA = clipbd.getcontents (cutandpaste.this);
            try {string clipstring = (string) clipdata.
        Gettransferdata (Dataflavor.stringflavor);
      Text.replacerange (clipstring, Text.getselectionstart (), Text.getselectionend ());
      catch (Exception ex) {System.out.println ("not String flavor");
    }} public static void Main (string[] args) {cutandpaste CP = new Cutandpaste (); Cp.addwindowlistEner (New Windowadapter () {public void windowclosing (WindowEvent e) {system.exit (0);
    }
      });
    Cp.setsize (300,200);
  Cp.setvisible (TRUE); }
} ///:~

Creating and adding menus and textarea to the present seems to have become a monotonous activity. This differs greatly from the CLIPBD of the Clipboard fields created by tool components.
All the movements are placed in the receiver. The Copyl and CUPL receivers also delete the copied lines except the final cutl line. A special two-line is the Setcontents () method from which the StringSelection object creates and invokes StringSelection from a string. To be more precise, a string is placed on the Clipboard.
In pastel, the data is decomposed by the clipboard using getcontents (). Any returned objects are removable and anonymous, and we don't really know what it contains. One way to find out is to call Gettransferdateflavors () and return an array of DataFlavor objects, indicating that special objects support this feature. We can also ask it to use isdataflavorsupported () directly through the characteristics we are interested in. But here's a bold approach: calling the Gettransferdata () method, assuming that the contents support string characteristics, and it's not a problem that is categorized in an exception handler.
In the future, we hope that more data features will be supported.

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.