Introduction
We have to create a JFrame that illustrates how to support undo and redo in components based on the JtextComponent class. In this Frame, we are going to create three JButtons and one JtextArea that implements the undo and redo operations. The label for the JButtons are “Undo”, “Redo” and “Exit”. Let us see in this program how the undo and redo operations are supported in JFC.
Working
This program actually consists of three classes,
- UndoDemo is a public class that will contain the main entry point of the program.
- UndoDemoPanel is a class derived from Jpanel, contains the user interface for the application.
- UndoableTextArea class is an extension of TextArea, which is an implementation of an undoable text area.
The main entry point creates a Jframe object and an instance of UndoDemo. The object of this class will be added to the frame. The constructor for UndoDemoPanel creates a toolbar with two buttons and an undoable text area (an instance of Undoable class). Then the listeners for the buttons call the undo and redo methods of the undoable text area object.
Note
Here the text variable representing the text area must be declared as final so that it can be accessed by anonymous inner classes that want to implement the button listeners.
The UndoableTextAarea class extends the JFC JTextArea class. In this class, the TextArea constructors are overridden with constructors that call a helper method, called initUndoable, to handle the initialization related to making the class undoable. Here the constructors will call the corresponding superclass constructor.
Source Code
UndoDemo.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.undo.*;
import java.util.Hashtable;
public class UndoDemo {
public static void main(String args[]) {
//create app panel
UndoDemoPanel panel = new UndoDemoPanel();
//Create a frame for app
JFrame frame = new JFrame("Undo and Redo Demo");
//Add a window listener for window close events
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
//Add app panel to content pane
frame.getContentPane().add(panel);
//Set initial frame size and make visible
frame.setSize(300, 200);
frame.setVisible(true);
}
}
UndoDemoPanel.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.undo.*;
import java.util.Hashtable;
class UndoDemoPanel extends JPanel {
public UndoDemoPanel() {
setLayout(new BorderLayout());
//create an undoable text area must be final to be accessed by //anonymous inner classes (listeners)
final UndoableTextArea text = new UndoableTextArea("Type your text here to test undo and redo operations");
//Create a toolbar for buttons
JToolBar toolbar = new JToolBar();
//Create undo and redo buttons and add listeners for buttons
JButton Undo, Redo, Exit;
Undo = new JButton("Undo");
Undo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
text.undo();
}
});
Redo = new JButton("Redo");
Redo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
text.redo();
}
});
Exit = new JButton("Exit");
Exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
});
//add buttons to toolbar
toolbar.add(Undo);
toolbar.add(Redo);
toolbar.add(Exit);
//add toolbar and text area to panel
add(toolbar, "North");
add(text, "Center");
}
}



Deepak KumarPosted Mar 12, 2022, 5:44 PM
What a fantastic post Sir! This article is so full of useful information I can't wait to dig deep into Java and start utilizing the resources you have given me. Your exuberance is refreshing.