I registered an event at JFrame
's root pane that reacts when hitting the space key (opens another window). I also have a JTextField
inside that JFrame. When the users is in edit mode of my textfield and hits the space key, the space event should be consumed only by the textfield and not be forwarded to the JFrame
's actionmap.
How do I do that?
Here is a runnable demo of the problem:
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
public class TestDialog {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0), "spaceAction");
frame.getRootPane().getActionMap().put("spaceAction", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("spaceAction");
}
});
JTextField tf = new JTextField("textfield");
JLabel label = new JLabel("otherComponent");
label.setFocusable(true);
frame.getContentPane().setLayout(new FlowLayout());
frame.getContentPane().add(tf);
frame.getContentPane().add(label);
frame.pack();
frame.setVisible(true);
}
}
It's not a good idea to use space key as a global trigger. But if you really need it here is the way:
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.text.JTextComponent;
public class DialogTest {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0), "spaceAction");
frame.getRootPane().getActionMap().put("spaceAction", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
if (EventQueue.getCurrentEvent() instanceof KeyEvent) {
KeyEvent ke = (KeyEvent) EventQueue.getCurrentEvent();
if (!(ke.getComponent() instanceof JTextComponent)) {
System.out.println("spaceAction");
} else {
System.out.println("Ignore event in text component");
}
} else {
System.out.println("spaceAction");
}
}
});
JTextField tf = new JTextField("textfield");
JLabel label = new JLabel("otherComponent");
label.setFocusable(true);
frame.getContentPane().setLayout(new FlowLayout());
frame.getContentPane().add(tf);
frame.getContentPane().add(label);
frame.pack();
frame.setVisible(true);
}
}
Better way is to travers the component tree starting from the root pane and add the key binding only for the components you need (for example all labels). Here is my method for traversal
/**
* Searches for all children of the given component which are instances of the given class.
*
* @param aRoot start object for search.
* @param aClass class to search.
* @param <E> class of component.
* @return list of all children of the given component which are instances of the given class. Never null.
*/
public static <E> List<E> getAllChildrenOfClass(Container aRoot, Class<E> aClass) {
final List<E> result = new ArrayList<E>();
final Component[] children = aRoot.getComponents();
for (final Component c : children) {
if (aClass.isInstance(c)) {
result.add(aClass.cast(c));
}
if (c instanceof Container) {
result.addAll(getAllChildrenOfClass((Container) c, aClass));
}
}
return result;
}