A sample app showing the use of UndoableToggleEdit and CompoundEdit : Undo Redo « Swing JFC « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Swing JFC » Undo RedoScreenshots 
A sample app showing the use of UndoableToggleEdit and CompoundEdit
A sample app showing the use of UndoableToggleEdit and CompoundEdit
 
/*
Java Swing, 2nd Edition
By Marc Loy, Robert Eckstein, Dave Wood, James Elliott, Brian Cole
ISBN: 0-596-00408-7
Publisher: O'Reilly 
*/
// UndoableToggleApp2.java
//A sample app showing the use of UndoableToggleEdit and CompoundEdit.
//

import java.awt.BorderLayout;
import java.awt.Container;
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.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import javax.swing.JToggleButton;
import javax.swing.undo.AbstractUndoableEdit;
import javax.swing.undo.CannotRedoException;
import javax.swing.undo.CannotUndoException;
import javax.swing.undo.CompoundEdit;
import javax.swing.undo.UndoableEdit;

public class UndoableToggleApp2 extends JFrame {

  private CompoundEdit edit;

  private JButton undoButton;

  private JButton redoButton;

  private JButton endButton;

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

    // Create some toggle buttons (and subclasses)
    JToggleButton tog = new JToggleButton("ToggleButton");
    JCheckBox cb = new JCheckBox("CompoundEdit ExampleCheckBox");
    JRadioButton radio = new JRadioButton("RadioButton");

    // Add our listener to each toggle button
    SimpleListener sl = new SimpleListener();
    tog.addActionListener(sl);
    cb.addActionListener(sl);
    radio.addActionListener(sl);

    // Lay out the buttons
    Box buttonBox = new Box(BoxLayout.Y_AXIS);
    buttonBox.add(tog);
    buttonBox.add(cb);
    buttonBox.add(radio);

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

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

    // Add a redo listener: just like the undo listener, but for redo this
    // time.
    redoButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ev) {
        try {
          edit.redo();
        catch (CannotRedoException ex) {
          ex.printStackTrace();
        finally {
          undoButton.setEnabled(edit.canUndo());
          redoButton.setEnabled(edit.canRedo());
        }
      }
    });

    // Add an end listener. This listener will call end() on the
    // CompoundEdit
    // and update the undo/redo buttons.
    endButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ev) {
        edit.end();
        endButton.setEnabled(false);
        undoButton.setEnabled(edit.canUndo());
        redoButton.setEnabled(edit.canRedo());
      }
    });

    // Layout the undo/redo/end buttons
    Box undoRedoEndBox = new Box(BoxLayout.X_AXIS);
    undoRedoEndBox.add(Box.createGlue());
    undoRedoEndBox.add(undoButton);
    undoRedoEndBox.add(Box.createHorizontalStrut(2));
    undoRedoEndBox.add(redoButton);
    undoRedoEndBox.add(Box.createHorizontalStrut(2));
    undoRedoEndBox.add(endButton);
    undoRedoEndBox.add(Box.createGlue());

    // Layout the main frame
    Container content = getContentPane();
    content.setLayout(new BorderLayout());
    content.add(buttonBox, BorderLayout.CENTER);
    content.add(undoRedoEndBox, BorderLayout.SOUTH);
    setSize(400150);
  }

  public class SimpleListener implements ActionListener {
    public void actionPerformed(ActionEvent ev) {
      if (edit == null || edit.isInProgress() == false)
        edit = new CompoundEdit();

      JToggleButton tb = (JToggleButtonev.getSource();
      UndoableEdit togEdit = new UndoableToggleEdit(tb);
      edit.addEdit(togEdit);
      endButton.setEnabled(true);
      undoButton.setEnabled(edit.canUndo());
      redoButton.setEnabled(edit.canRedo());
    }
  }

  // Main program just creates the frame and displays it.
  public static void main(String[] args) {
    JFrame f = new UndoableToggleApp2();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
  }
}

//UndoableToggleEdit.java
//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 {
    super.redo();
    button.setSelected(selected);
  }

  // Undo by setting the button state to the opposite value.
  public void undo() throws CannotUndoException {
    super.undo();
    button.setSelected(!selected);
  }
}

           
         
  
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. 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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.