How to fix accelerators (keyboard shortcuts) not working with Swing actions
Let’s start with a piece of code that seems to be correct at the first glance…
This snippet was generated by Eclipse WindowBuilder.
Looks good to you? Well… It’s not. The menu item will not have an accelerator key combination.
Always check autogenerated code if you can.
The problem with this code is that Swing actions have their own accelerator key property and when you set the action for a menu item, that menu’s accelerator gets replaced with the action’s accelerator. In other words, the snippet above is equivalent to this:
The obvious solution would be to put setAccelerator after setAction, like this:
But that is by far not the best way to do this! A better way to do this would be to set the action’s accelerator key stroke.
Your action’s constructor might look like this:
Let’s add the accelerator key:
Now, we don’t need to call setAccelerator on mntmTest because it will be called automatically in setAction.