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);
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.