Reputation: 503
I try this simple code and System.out.println
works when zoom button not pressed. But after pressing zoom button System.out.println
not works. Why?
zoomFree.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
isZoomFree = true;
chartPanel.setDomainZoomable(true);
chartPanel.setRangeZoomable(true);
zoomIn.setEnabled(false);
zoomFree.setEnabled(false);
}
});
Action escapeZoom = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("ESCAPE pressed! yahoo!");
}
};
panel.getInputMap().put(KeyStroke.getKeyStroke("ESCAPE"), "escape pressed");
panel.getActionMap().put("escape pressed", escapeZoom);
Upvotes: 1
Views: 96
Reputation: 22233
When you pass no arguments to the getInputMap
method, your key bindings will work only if the component is focused.
Try using getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
instead:
panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("ESCAPE"), "escape pressed");
panel.getActionMap().put("escape pressed", escapeZoom);
This way the component doesn't need to have the focus, they will work when the window has the focus
Upvotes: 3