The code below is part of a method that creates a JPopupMenu
(not shown) which has a few options like undo, redo, cut, copy, paste, etc...
JMenuItem redoItem = new JMenuItem(new PrintAction());
redoItem.setText("Redo");
redoItem.setMnemonic(KeyEvent.VK_Y);
popupMenu.add(redoItem);
JMenuItem cutItem = new JMenuItem(new DefaultEditorKit.CutAction());
cutItem.setText("Cut");
cutItem.setMnemonic(KeyEvent.VK_X);
popupMenu.add(cutItem);
The PrintAction
class is only used for debugging purposes, but this is where I'd normally put the RedoAction
class
public class PrintAction extends AbstractAction {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
System.out.println("Yay it worked!");
}
}
As you can see, I've set the mnemonic for the "Redo" action as Y, and the "Cut" action as X.
I'm using the built-in method CutAction
from the DefaultEditorKit
, which automatically sets my modifier to control, and cuts the text exactly when I want it. (CTRL + X)
Here's the issue: since my redoItem
does NOT use DefaultEditorKit, it defaults the modifier to ALT, and only redos the text when the popupMenu is showing. (ALT + Y)
cutItem
works exactly the way I want it. How should I make the redoItem
with the same features? I want (CTRL + Y) instead of (ALT + Y) and to use the action without having popupMenu
open.
I have read the forum with a similar title here, but it does not have a proper answer.