[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

undo/redo

Posted on 2005-04-20
3
Medium Priority
?
707 Views
Last Modified: 2008-02-01
I’m trying to put undo/redo facility to JEditorPne using the example code I found here.
http://javaalmanac.com/egs/javax.swing.undo/UndoText.html


However it does not do the job and it seems to be that the if condition in actionPerformed() always seems to return false.

if (undo.canUndo()) { //always return false;
                        undo.undo();
                    }

Can any one tell me what correction needs to be done in order it to work?
Here is my code
  JEditorPane editor = new JEditorPane();

          //undo redo         
           final UndoManager undo = new UndoManager();
    Document doc = editor.getDocument();
   
    // Listen for undo and redo events
    doc.addUndoableEditListener(new UndoableEditListener() {
        public void undoableEditHappened(UndoableEditEvent evt) {
            undo.addEdit(evt.getEdit());
        }
    });
   
    // Create an undo action and add it to the text component
    editor.getActionMap().put("Undo",
        new AbstractAction("Undo") {
            public void actionPerformed(ActionEvent evt) {
                try {
                             System.out.println("undo()");
                  //  if (undo.canUndo()) {
                        undo.undo();
                       
                             
                 
                 //   }
                } catch (CannotUndoException e) {
                      e.printStackTrace();
                }
            }
       });
       
          // Bind the undo action to ctl-Z
    editor.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_Z, InputEvent.CTRL_MASK), "Undo");
   
    // Create a redo action and add it to the text component
    editor.getActionMap().put("Redo",
        new AbstractAction("Redo") {
            public void actionPerformed(ActionEvent evt) {
                try {
                          
                        System.out.println("redo()");
                //    if (undo.canRedo()) {
                        undo.redo();
                       
                   
                 //   }
                } catch (CannotRedoException e) {
                      e.printStackTrace();
                }
            }
        });

   
// Bind the redo action to ctl-Y
    editor.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_Y, InputEvent.CTRL_MASK), "Redo");

0
Comment
Question by:csound
  • 2
3 Comments
 
LVL 15

Expert Comment

by:aozarov
ID: 13830495
Are you sure this method:
public void undoableEditHappened(UndoableEditEvent evt) {
            undo.addEdit(evt.getEdit());
        }
get called...?
0
 

Author Comment

by:csound
ID: 13832035
actually it does not get called at all...
do you know how should it be?
0
 
LVL 15

Accepted Solution

by:
aozarov earned 200 total points
ID: 13836126
So that explains your problem (no change no need for undo ;-)
You will need to create unable events
see the following link explanation and example: http://www.iam.ubc.ca/guides/javatut99/uiswing/events/undoableeditlistener.html
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses
Course of the Month20 days, 8 hours left to enroll

868 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question