Undo redo textarea : Undo Redo « Swing JFC « Java






Undo redo textarea

Undo redo textarea
 
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener;
import javax.swing.undo.CannotRedoException;
import javax.swing.undo.UndoManager;

public class UndoRedoTextArea extends JFrame {
  protected JTextArea textArea = new JTextArea();

  protected UndoManager undoManager = new UndoManager();

  protected JButton undoButton = new JButton("Undo");

  protected JButton redoButton = new JButton("Redo");

  public UndoRedoTextArea() {
    super("Undo/Redo Demo");

    undoButton.setEnabled(false);
    redoButton.setEnabled(false);

    JPanel buttonPanel = new JPanel(new GridLayout());
    buttonPanel.add(undoButton);
    buttonPanel.add(redoButton);

    JScrollPane scroller = new JScrollPane(textArea);

    getContentPane().add(buttonPanel, BorderLayout.NORTH);
    getContentPane().add(scroller, BorderLayout.CENTER);

    textArea.getDocument().addUndoableEditListener(
        new UndoableEditListener() {
          public void undoableEditHappened(UndoableEditEvent e) {
            undoManager.addEdit(e.getEdit());
            updateButtons();
          }
        });

    undoButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        try {
          undoManager.undo();
        } catch (CannotRedoException cre) {
          cre.printStackTrace();
        }
        updateButtons();
      }
    });

    redoButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        try {
          undoManager.redo();
        } catch (CannotRedoException cre) {
          cre.printStackTrace();
        }
        updateButtons();
      }
    });

    setSize(400, 300);
    setVisible(true);
  }

  public void updateButtons() {
    undoButton.setText(undoManager.getUndoPresentationName());
    redoButton.setText(undoManager.getRedoPresentationName());
    undoButton.setEnabled(undoManager.canUndo());
    redoButton.setEnabled(undoManager.canRedo());
  }

  public static void main(String argv[]) {
    new UndoRedoTextArea();
  }
}
           
         
  








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.The use of UndoManagerThe use of UndoManager
4.A sample app showing the use of UndoableToggleEdit and CompoundEditA sample app showing the use of UndoableToggleEdit and CompoundEdit
5.An example that shows lots of little UndoManager detailsAn example that shows lots of little UndoManager details
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