java left logo
java middle logo
java right logo
 

Home arrow Java SE Tips
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4093
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
How to implement an undo option for Style manipulation E-mail
User Rating: / 14
PoorBest 

This Java Swing tip illustrates a method of implementing an undo option for Style manipulation. For example add undo support to the StyleFrame example described in tip titled "How to implement different style options in your application". This tip only retains the most recent edit, to keep things simple.

import javax.swing.*;
import javax.swing.undo.*;
import javax.swing.event.*;
import java.awt.event.*;

public class UndoStyleFrame extends StyleFrame {

  protected UndoAct undoAction = new UndoAct()// an Action for undo
  protected RedoAct redoAction = new RedoAct()// an Action for redo

  public UndoStyleFrame() {
    super();
    setTitle("UndoStyleFrame");

    // register the Actions as undo listeners (we inherited textPane)
    textPane.getDocument().addUndoableEditListener(undoAction);
    textPane.getDocument().addUndoableEditListener(redoAction);

    // create menu for undo/redo
    JMenu editMenu = new JMenu("Edit");
    editMenu.add(new JMenuItem(undoAction));
    editMenu.add(new JMenuItem(redoAction));
    menuBar.add(editMenu)// we inherited menuBar from superclass

    // create buttons for undo/redo
    JPanel buttonPanel = new JPanel();
    buttonPanel.add(new JButton(undoAction));
    buttonPanel.add(new JButton(redoAction));
    getContentPane().add(buttonPanel, java.awt.BorderLayout.SOUTH);
  }

  // begin inner classes ------------
  
  public class UndoAct extends AbstractAction implements UndoableEditListener {
    private UndoableEdit edit;

    public UndoAct() {
      super("Undo");
      setEnabled(false);
    }
    public void updateEnabled() {
      setEnabled(edit.canUndo());
    }
    public void undoableEditHappened(UndoableEditEvent event) {
      edit = event.getEdit();
      putValue(NAME, edit.getUndoPresentationName());
      updateEnabled();
    }
    public void actionPerformed(ActionEvent ae) {
      edit.undo();
      updateEnabled()// disable undo
      redoAction.updateEnabled()// enable redo
    }
  }
  
  public class RedoAct extends AbstractAction implements UndoableEditListener {
    private UndoableEdit edit;

    public RedoAct() {
      super("Redo");
      setEnabled(false);
    }
    public void updateEnabled() {
      setEnabled(edit.canRedo());
    }
    public void undoableEditHappened(UndoableEditEvent event) {
      edit = event.getEdit();
      putValue(NAME, edit.getRedoPresentationName());
      updateEnabled();
    }
    public void actionPerformed(ActionEvent ae) {
      edit.redo();
      updateEnabled()// disable redo
      undoAction.updateEnabled()// enable undo
    }
  }
  
  // end inner classes ------------

  public static void main(String[] args) {
    JFrame frame = new UndoStyleFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400300);
    frame.setVisible(true);
  }
}

 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.