As part of an annotation program, I have an excerpt of code which is responsible for undoing the last point plotted on an image:
Action undoListener = new AbstractAction(){
public void actionPerformed(ActionEvent actionEvent){
System.out.println(actionEvent.getActionCommand());
imagePanel.undoLastPoint();
}
};
JButton undoPointButton = new JButton("Undo Last Point");
undoPointButton.addActionListener(undoListener);
KeyStroke ctrlZKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_Z, ActionEvent.CTRL_MASK);
JPanel thisContentPane = (JPanel)this.getContentPane();
thisContentPane.getInputMap().put(ctrlZKeyStroke, "undo");
thisContentPane.getActionMap().put("undo", undoListener);
As you can see, the user can activate the undo
action by either clicking the undo button or by pressing Ctrl+Z on the keyboard. I was interested to see what the action command returned for each of these methods showed. For the button click, the output is sensible - It prints "Undo Last Point". For the keyboard shortcut however, it prints a strange 2X2 grid of binary digits to the terminal.
I am wondering whether there is a way to explicitly set what action command is returned by the keyboard shortcut, as it might be useful to differentiate between the two or perhaps show that they represent the same thing.
I have looked up the Java API for InputMap
, ActionMap
, and KeyStroke
but have not managed to find any clues. Perhaps the problem is that I am thinking about what actionCommand
is and what it is for in completely the wrong way?