The use of UndoManager : Undo Redo « Swing JFC « Java

The use of UndoManager

The use of UndoManager
Java Swing, 2nd Edition
By Marc Loy, Robert Eckstein, Dave Wood, James Elliott, Brian Cole
ISBN: 0-596-00408-7
Publisher: O'Reilly 
//A sample app showing the use of UndoManager.

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JToggleButton;
import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener;
import javax.swing.undo.AbstractUndoableEdit;
import javax.swing.undo.CannotRedoException;
import javax.swing.undo.CannotUndoException;
import javax.swing.undo.UndoManager;

public class UndoableToggleApp3 extends JFrame {

  private UndoManager manager = new UndoManager();

  private JButton undoButton;

  private JButton redoButton;

  // Create the main frame and everything in it.
  public UndoableToggleApp3() {

    // Create some toggle buttons.
    UndoableJToggleButton tog1 = new UndoableJToggleButton("One");
    UndoableJToggleButton tog2 = new UndoableJToggleButton("Two");
    UndoableJToggleButton tog3 = new UndoableJToggleButton("Three");

    // Add our listener to each toggle button.
    SimpleUEListener sl = new SimpleUEListener();

    // Lay out the buttons.
    Box buttonBox = new Box(BoxLayout.Y_AXIS);

    // Create undo and redo buttons (initially disabled).
    undoButton = new JButton("Undo");
    redoButton = new JButton("Redo");

    // Add a listener to the undo button. It attempts to call undo() on the
    // UndoManager, then enables/disables the undo/redo buttons as
    // appropriate.
    undoButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ev) {
        try {
        } catch (CannotUndoException ex) {
        } finally {

    // Add a redo listener: just like the undo listener.
    redoButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ev) {
        try {
        } catch (CannotRedoException ex) {
        } finally {

    // Lay out the undo/redo buttons.
    Box undoRedoBox = new Box(BoxLayout.X_AXIS);

    // Lay out the main frame.
    getContentPane().setLayout(new BorderLayout());
    getContentPane().add(buttonBox, BorderLayout.CENTER);
    getContentPane().add(undoRedoBox, BorderLayout.SOUTH);
    setSize(400, 150);

  public class SimpleUEListener implements UndoableEditListener {
    // When an UndoableEditEvent is generated (each time one of the buttons
    // is pressed), we add it to the UndoManager and then get the manager's
    // undo/redo names and set the undo/redo button labels. Finally, we
    // enable/disable these buttons by asking the manager what we are
    // allowed to do.
    public void undoableEditHappened(UndoableEditEvent ev) {

  // Method to set the text and state of the undo/redo buttons.
  protected void updateButtons() {

  // Main program just creates the frame and displays it.
  public static void main(String[] args) {
    JFrame f = new UndoableToggleApp3();

//Sample undoable toggle button class. Supports only a single listener to
//simplify the code.

class UndoableJToggleButton extends JToggleButton {
  private UndoableEditListener listener;

  // For this example, we'll just provide one constructor . . .
  public UndoableJToggleButton(String txt) {

  // Set the UndoableEditListener.
  public void addUndoableEditListener(UndoableEditListener l) {
    listener = l; // Should ideally throw an exception if listener != null

  // Remove the UndoableEditListener.
  public void removeUndoableEditListener(UndoableEditListener l) {
    listener = null;

  // We override this method to call the super implementation first (to fire
  // the
  // action event) and then fire a new UndoableEditEvent to our listener.
  protected void fireActionPerformed(ActionEvent ev) {

    // Fire the ActionEvent as usual.

    if (listener != null) {
      listener.undoableEditHappened(new UndoableEditEvent(this,
          new UndoableToggleEdit(this)));

//An UndoableEdit used to undo the pressing of a JToggleButton.

class UndoableToggleEdit extends AbstractUndoableEdit {

  private JToggleButton button;

  private boolean selected;

  // Create a new edit for a JToggleButton that has just been toggled.
  public UndoableToggleEdit(JToggleButton button) {
    this.button = button;
    selected = button.isSelected();

  // Return a reasonable name for this edit.
  public String getPresentationName() {
    return "Toggle " + button.getText() + " " + (selected ? "on" : "off");

  // Redo by setting the button state as it was initially.
  public void redo() throws CannotRedoException {

  // Undo by setting the button state to the opposite value.
  public void undo() throws CannotUndoException {


Related examples in the same category

1.The use of UndoableToggleEditThe use of UndoableToggleEdit
2.The use of StateEdit(able)The use of StateEdit(able)
3.A sample app showing the use of UndoableToggleEdit and CompoundEditA sample app showing the use of UndoableToggleEdit and CompoundEdit
4.An example that shows lots of little UndoManager detailsAn example that shows lots of little UndoManager details
5.Undo redo textareaUndo redo textarea
6.Undo managerUndo manager
7.Simple GUI demo of UndoManager and friendsSimple GUI demo of UndoManager and friends
8.Undo Example 1Undo Example 1
9.Undo Example 2Undo Example 2
10.Undo Example 3Undo Example 3
11.Undo Example 4Undo Example 4
12.Undo Example 5Undo Example 5
13.Undo Example 6Undo Example 6
14.Undoable Drawing Panel 2Undoable Drawing Panel 2
15.Undo DrawingUndo Drawing
16.Undo Example 7Undo Example 7
17.Creating TextArea with Undo, Redo Capabilities
18.Add Undo and Redo to a text component
19.Add undo support to the StyleFrame exampleAdd undo support to the StyleFrame example
20.Adding Undo and Redo to a Text Component
21.Create a redo action and add it to the text component (JTextComponent)
22.Listen for undo and redo events
23.Create an undo action and add it to the text component
24.Bind the undo action to ctl-Z