Design A Frame That Has An Edit Menu With Cut, Copy And Paste Functions In Java

Introduction

In this article, I'll create a frame application that has an Edit menu. This menu contains Cut, Copy and Paste as menu items and does similar activities. Ii includes a text area into which text can be entered and the cut, copy and paste activities can be demonstrated.

Working

Java supports limited operations with the system clipboard. It is possible to cut/copy string of our selection and paste it onto string objects. These clipboards hold any type of data, the classes involved to do this are the Clipboard, the DataFlavor, and the StringSelection class belonging to the datatransfer package. The Clipboard class implements a mechanism to transfer data using cut/copy and paste. The java API currently provides support for String type data but has extensibility through the concept of flavor. The class DataFlavor is used to represent the opaque concept of a data format as it would appear on clipboard, during drag and drop, or in a file system. ‘When the data comes off a clipboard, it has an associated set of flavors that can be converted to. The StringSelection class implements the capability required to transfer a simple Java String in plain text format.

The following program demonstrates the cut, copy and paste with String data in a TextArea.

Source Code

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 selection = 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 clipData = 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.setSize(300, 200);
        cp.setVisible(true);
        cp.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent w) {
                System.exit(0);
            }
        });
    }
}

Compile the above source code as,

Design A Frame That Has An Edit Menu With Cut Copy And Paste Functions In Java

Run the source code

E:\Java>java CutAndPaste

Design A Frame That Has An Edit Menu With Cut Copy And Paste Functions In Java

Output

Design A Frame That Has An Edit Menu With Cut Copy And Paste Functions In Java

Type any Document in the textarea and perform the operations Cut/Copy and Paste…

Design A Frame That Has An Edit Menu With Cut Copy And Paste Functions In Java

And perform finally paste operation in any portion inside textarea…

Design A Frame That Has An Edit Menu With Cut Copy And Paste Functions In Java

Summary

The java.awt.datatransfer package contains classes & interfaces which defines a framework for user-driven inter-application or intra-application data transfer. Also contains a no. of classes that support data transfer through Cut & Paste.