How to implement Cut, Copy, Paste, Undo, Redo in java swing?

UPDATED: 24 May 2013
I was exploring the folders and found my college project. I thought let me write article for old school project. It might help students working for first time in java swing. Its very easy to implement Cut, Copy, Paste, Undo, Redo when you deal with text input(s).

Cut, Copy, Paste, Undo, Redo in java swing

I took jTextarea in this example. I'll give you core script but you have to modify the code as per your need. Like onclick event, KeyStroke, etc...

Code for handling cut event
 /* Below code for netbeans */ 
 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        jTextArea1.cut();
 }

 /* Below code when you want to attach action listener */
 class cut implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            jTextArea1.cut();
        }
 }

Code for handling copy event
 /* Below code for netbeans */ 
 private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
        jTextArea1.copy();
 }

 /* Below code when you want to attach action listener */
 class copy implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            jTextArea1.copy();
        }
 }

Code for handling paste event
 /* Below code for netbeans */ 
 private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
        jTextArea1.paste();
 }

 /* Below code when you want to attach action listener */
 class paste implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            jTextArea1.paste();
        }
 }

To implement Undo and Redo you have to run code with in constructor. Below code will implement UndoableEditListener for jTextarea.
import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener;
import javax.swing.undo.CannotRedoException;
import javax.swing.undo.UndoManager;

/* Create global variable */
UndoManager undo = new UndoManager();

jTextArea1.getDocument().addUndoableEditListener(
                new UndoableEditListener() {
                    public void undoableEditHappened(UndoableEditEvent e) {
                        undo.addEdit(e.getEdit());
                }
});

Code for handling undo event
 /* Below code for netbeans */ 
 private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
        try {
            undo.undo();
        } catch (CannotRedoException cre) {
            cre.printStackTrace();
        }
 }

 /* Below code when you want to attach action listener */
 class undo implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            try {
               undo.undo();
            } catch (CannotRedoException cre) {
               cre.printStackTrace();
            }
        }
 }

Code for handling redo event
 /* Below code for netbeans */ 
 private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
        try {
            undo.redo();
        } catch (CannotRedoException cre) {
            cre.printStackTrace();
        }
 }

 /* Below code when you want to attach action listener */
 class redo implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            try {
               undo.redo();
            } catch (CannotRedoException cre) {
               cre.printStackTrace();
            }
        }
 }

Source Code

0 comments :