X

Geertjan's Blog

  • September 19, 2007

Enabling Undo/Redo Functionality on the NetBeans Platform

Geertjan Wielenga
Product Manager
I've been looking through a draft of Heiko Böck's "NetBeans 6 - Rich-Client-Entwicklung mit Java" (which you can already preorder from Amazon, right here). Lots of interesting bits of code in there, quite a lot that I hadn't seen before. Some of them involve the UndoRedo.Manager. The basics are quite simple—declare the manager, override the getUndoRedo() method in the TopComponent or MultiviewElement and then, as shown in the highlighted line in the constructor below, assign the manager as an UndoableEditListener to the component's Document object:

private UndoRedo.Manager manager = new UndoRedo.Manager();
private DemoTopComponent() {
initComponents();
setName(NbBundle.getMessage(DemoTopComponent.class, "CTL_DemoTopComponent"));
setToolTipText(NbBundle.getMessage(DemoTopComponent.class, "HINT_DemoTopComponent"));jEditorPane1.getDocument().addUndoableEditListener(manager);
}
@Override
public UndoRedo getUndoRedo() {
return manager;
}

Once you've done that, the IDE's (or your own application's) Undo and Redo functionality is enabled for your component—the menu items and toolbar buttons are enabled when you expect them to be, i.e., after making changes to the component's document, such as in the JEditorPane in the example below, where changes to the "Failed New Year's Resolutions" list result in the undo/redo actions being enabled/disabled:

I know there are more complex scenarios, and that those scenarios are often a cause of frustration, but at least, in this simple scenario, I'm happy to say that things work really nicely and exactly as I would expect, and clearly for very little coding.

In other news. Recently, the NetBeans Web team made a small but significant improvement—they've provided a constant URL pointing to the latest NetBeans weekly newsletter. In the past, each week's newsletter had a different URL, based on the day the newsletter was published. So, in the left sidebar of this blog, where I have a link to the latest newsletter, I used to have to change that link each week. Now there's just a standard URL that will always bring you to the latest newsletter. Why not add it to your blog or correspondence, or wherever, to publicize this extremely useful resource, that will always keep you up to date with the latest and coolest info coming out of the NetBeans project? Here it is: http://www.netbeans.org/community/news/newsletter/latest.html.

Join the discussion

Comments ( 7 )
  • Tom Wheeler Wednesday, September 19, 2007

    Good luck on ascending the Spanish throne! El Rey Geertjan has a nice ring to it.


  • Pedro Dias Wednesday, March 19, 2008

    I successfully added undo/redo to my project but now i cant get the cut/copy/paste buttons to work, could you give me any idea how to do it?

    They worked once but only when the selected topcomponent was the last one created and now even this work dont work.


  • Dave Irving Tuesday, June 24, 2008

    I tried this, but I do not have a Document per say. Just created a method to add the UndoRedoListener to another class, and could see the Undo events being sent to the manager. However, the toolbar buttons do not get enabled. IS there something else I should be doing.


  • Tom Borak Thursday, June 18, 2009

    I was just reading this excellent book and creating small projects to test the concepts. I was able to add the Cut/Copy/Paste to my sample app, but didn't know how to incorporate the undo/redo functionality until I read your post. It couldn't be any easier (for my simple example)! Thank you.


  • Geertjan Thursday, June 18, 2009

    Good to hear, thanks!


  • guest Wednesday, March 27, 2013

    In a diferent scenario like drawing people can take this aproach:

    http://www.java2s.com/Code/JavaAPI/javax.swing.undo/UndoableEditSupportpostEditUndoableEdite.htm

    Good coding :)


  • guest Thursday, June 11, 2015

    For all people, who are looking to implement Undo/Redo for arbitrary components (those that don't provide an addUndoableEditListener() method), look at this https://blogs.oracle.com/geertjan/entry/undoredo_in_visual_library_scenes blog post, which is harder to find on google. It gives a better general idea of how to use an UndoRedo.Manager.


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.