Posted on 2005-04-20
Last Modified: 2008-02-01
I’m trying to put undo/redo facility to JEditorPne using the example code I found here.

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;

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) {
    // Create an undo action and add it to the text component
        new AbstractAction("Undo") {
            public void actionPerformed(ActionEvent evt) {
                try {
                  //  if (undo.canUndo()) {
                 //   }
                } catch (CannotUndoException e) {
          // 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
        new AbstractAction("Redo") {
            public void actionPerformed(ActionEvent evt) {
                try {
                //    if (undo.canRedo()) {
                 //   }
                } catch (CannotRedoException e) {

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

Question by:csound
    LVL 15

    Expert Comment

    Are you sure this method:
    public void undoableEditHappened(UndoableEditEvent evt) {
    get called...?

    Author Comment

    actually it does not get called at all...
    do you know how should it be?
    LVL 15

    Accepted Solution

    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:

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Live - One-on-One Java Help from Top Experts

    Solve your toughest problems, fast.
    Java experts are online now and ready to help you.

    Suggested Solutions

    Title # Comments Views Activity
    Java 1603 Error 2 26
    seriesUp challenge 7 79
    nested class vs inner class 5 36
    pairstar challenge 2 26
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
    Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
    This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now