FileDocCategorySizeDatePackage
UndoableToggleApp3.javaAPI DocExample3644Thu Oct 24 20:14:26 BST 2002None

UndoableToggleApp3

public class UndoableToggleApp3 extends JFrame

Fields Summary
private UndoManager
manager
private JButton
undoButton
private JButton
redoButton
Constructors Summary
public UndoableToggleApp3()


  // Create the main frame and everything in it.
    

    // 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();
    tog1.addUndoableEditListener(sl);
    tog2.addUndoableEditListener(sl);
    tog3.addUndoableEditListener(sl);

    // Lay out the buttons.
    Box buttonBox = new Box(BoxLayout.Y_AXIS);
    buttonBox.add(tog1);
    buttonBox.add(tog2);
    buttonBox.add(tog3);

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

    // 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 {
          manager.undo();
        } catch (CannotUndoException ex) { ex.printStackTrace(); }
        finally {
          updateButtons();
        }
      }
    });

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

    // Lay out the undo/redo buttons.
    Box undoRedoBox = new Box(BoxLayout.X_AXIS);
    undoRedoBox.add(Box.createGlue());
    undoRedoBox.add(undoButton);
    undoRedoBox.add(Box.createHorizontalStrut(2));
    undoRedoBox.add(redoButton);
    undoRedoBox.add(Box.createGlue());

    // Lay out the main frame.
    getContentPane().setLayout(new BorderLayout());
    getContentPane().add(buttonBox, BorderLayout.CENTER);
    getContentPane().add(undoRedoBox, BorderLayout.SOUTH);
    setSize(400, 150);
  
Methods Summary
public static voidmain(java.lang.String[] args)

    JFrame f = new UndoableToggleApp3();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
  
protected voidupdateButtons()

    undoButton.setText(manager.getUndoPresentationName());
    redoButton.setText(manager.getRedoPresentationName());
    undoButton.getParent().validate();
    undoButton.setEnabled(manager.canUndo());
    redoButton.setEnabled(manager.canRedo());