• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
  • salvin francis
  • Frits Walraven
  • Piet Souris

javax.swing.undo.UndoManager & javax.swing.JMenuItem

Ranch Hand
Posts: 42
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone explain what I am missing and why this is not working? I am not going to paste the whole class as it's 1500 lines.

While it works using short cut keys example:
GOOD CODE FOR Keyboard short-cut

I am now trying to add/use the JMenuItem with the following code and I can't understand why this is not working as it looks all good too me.


I have no error actually System.out.println("trying"); shows when button is pressed but for some reason undoManager.canUndo() is not valid.

Any help would be greatly appreciated as I am sure it's something simple I forgot or can't see at the moment.
Posts: 3213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your basic approach is wrong.

The benefit of using an Action is that it can be shared by any component that supports Actions.

So instead of trying to implement multiple annonymous inner classes, you should be creating a single Action. Then you can create your JMenuItem with this Action.

Another benefit of using the Action is that you can assign the accelerator and mnemonic keys directly to the Action. Swing will then do the Keystroke bindings for you automatically when you create the menu item.

Read the section from the Swing tutorial on How to Use Actions for more information. There is also a section on Text Component Features that demos  working undo/redo Actions.
    Bookmark Topic Watch Topic
  • New Topic