Undo Text using Ctrl+Z in Java
In this tutorial, I will teach you how to build a program that can undo a text using a shortcut key of ctrl+z in java.
So, now let’s start this tutorial!
1. Open JCreator or NetBeans and make a java program with a file name of undoCtrlZ.java.
2. Import the following package library:
import java.awt.event.*; //used to access ActionEvent class import javax.swing.*; //used to access AbstractAction,JFrame, JScrollPane, JTextArea, and KeyStroke class import javax.swing.event.*; // used to access UndoableEditEvent and UndoableEditListener class import javax.swing.text.*;// used to access the Document and JTextComponent class import javax.swing.undo.*; // used to access CannotUndoException and UndoManager class
3. We will initialize variables in our Main, variable frame as JFrame, txtArea for JTextArea, undo as UndoManager and declare it as final because we will make an inner class on it, variable doc for the Document class.
JFrame frame = new JFrame(); //instantiate frame JTextArea txtArea = new JTextArea(); //instantiate textArea final UndoManager undo = new UndoManager(); //instantiate an UndoManager Document doc = txtArea.getDocument(); //instantiate a Document class of the txtArea
4. Make an inner class for the UndoableEditEvent of the doc in our textArea. This will trigger the textArea and can be editable.
doc.addUndoableEditListener(new UndoableEditListener() { public void undoableEditHappened(UndoableEditEvent evt) { undo.addEdit(evt.getEdit()); } });
5. Make an inner class that will have an undo event on the textArea. This will use the getActionMap method.
txtArea.getActionMap().put("Undo", new AbstractAction("Undo") { public void actionPerformed(ActionEvent evt) { try { if (undo.canUndo()) { undo.undo(); } } catch (CannotUndoException e) { } } });
To have a ctrl z shortcut for the undo. Copy this code below:
txtArea.getInputMap().put(KeyStroke.getKeyStroke("control Z"), "Undo");
6. Finally, add the components, set the size and visibility to true, and close its operation.
txtArea.getInputMap().put(KeyStroke.getKeyStroke("control Z"), "Undo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new JScrollPane(txtArea)); frame.setSize(380, 320); frame.setLocationRelativeTo(null); frame.setVisible(true);
Output:
Here’s the full code of this tutorial:
import java.awt.event.*; //used to access ActionEvent class import javax.swing.*; //used to access AbstractAction,JFrame, JScrollPane, JTextArea, and KeyStroke class import javax.swing.event.*; // used to access UndoableEditEvent and UndoableEditListener class import javax.swing.text.*;// used to access the Document and JTextComponent class import javax.swing.undo.*; // used to access CannotUndoException and UndoManager class public class undoCtrlZ { public static void main(String[] args) { JFrame frame = new JFrame("Ctrl+Z Undo"); //instantiate frame JTextArea txtArea = new JTextArea(); //instantiate textArea final UndoManager undo = new UndoManager(); //instantiate an UndoManager Document doc = txtArea.getDocument(); //instantiate a Document class of the txtArea doc.addUndoableEditListener(new UndoableEditListener() { public void undoableEditHappened(UndoableEditEvent evt) { undo.addEdit(evt.getEdit()); } }); txtArea.getActionMap().put("Undo", new AbstractAction("Undo") { public void actionPerformed(ActionEvent evt) { try { if (undo.canUndo()) { undo.undo(); } } catch (CannotUndoException e) { } } }); txtArea.getInputMap().put(KeyStroke.getKeyStroke("control Z"), "Undo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new JScrollPane(txtArea)); frame.setSize(380, 320); frame.setLocationRelativeTo(null); frame.setVisible(true); } }