Java Swing Undo Redo Operations


 

Java Swing Undo Redo Operations

In this section, you will learn how to implement Undo Redo Opeartions in java swing.

In this section, you will learn how to implement Undo Redo Opeartions in java swing.

Java Swing Undo Redo Operations

Swing provides Undo and Redo functionality through javax.swing.undo package. The undo mechanisms allow users to unexecute the last action they just performed and using redo, user can re-execute the last undone action. Now, in order to implement these mechanisms, we have created a text area in our class that implements UndoableEditListener interface from javax.swing.event package for getting undoable edit events. The method undoableEditHappened(UndoableEditEvent e) is called to notify the listeners that the undoable edits happened. The typical action in this method is to add the edits to the UndoManager . The UndoManager collects all the edits happened and through the buttons, we have called undo() method of UndoManager to undo the changes and redo() method to redo changes.

Here is the code:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.undo.*;
import javax.swing.text.*;
import javax.swing.event.*;

public class UndoAndRedoOperations extends JFrame {
	JButton b1, b2, b3;
	JTextArea area;
	JScrollPane pane;
	JPanel p;
	UndoManager manager = new UndoManager();

	public UndoAndRedoOperations() {
		p = new JPanel();
		area = new JTextArea(5, 30);
		pane = new JScrollPane(area);
		manager = new UndoManager();
		b1 = new JButton("Undo");
		b2 = new JButton("Redo");
		b3 = new JButton("Exit");
		b1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					manager.undo();
				} catch (Exception ex) {
				}
			}
		});
		b2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					manager.redo();
				} catch (Exception ex) {
				}
			}
		});
		b3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		area.getDocument().addUndoableEditListener(new UndoableEditListener() {
			public void undoableEditHappened(UndoableEditEvent e) {
				manager.addEdit(e.getEdit());
			}
		});
		p.add(pane);
		p.add(b1);
		p.add(b2);
		p.add(b3);
		add(p);
		setVisible(true);
		pack();
	}

	public static void main(String[] args) {
		UndoAndRedoOperations op = new UndoAndRedoOperations();
	}
}

Output:

Ads