Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>By default, <code>JToolBar</code> registers an action for the KeyStroke's UP/DOWN/LEFT/RIGHT, and the <code>JToolBar</code> you add to the <code>JFrame</code> automatically grabs the focus, hence you don't see anything when binding to the rootpane (the toolbar catches the events before you).</p> <p>One solution is to make the <code>JToolBar</code> and the <code>JButton</code> not focusable and allow your <code>JPanel</code> to be focusable (actually you could leave the <code>JToolBar</code> and the <code>JButton</code> and request the focus on your panel but this also means that you have to handle the focus management of your panel):</p> <pre><code>import java.awt.Color; import java.awt.GridBagLayout; import java.awt.event.ActionEvent; import java.awt.event.KeyEvent; import javax.swing.AbstractAction; import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JToolBar; import javax.swing.KeyStroke; public class ArrowTest extends JFrame { public static void main(final String[] args) { final ArrowTest at = new ArrowTest(); at.setSize(100, 200); final JPanel jp = new JPanel(); jp.setBackground(Color.BLUE); jp.setFocusable(true); at.getContentPane().add(jp); final JToolBar toolbar = new JToolBar(); JButton comp = new JButton(); toolbar.add(comp); at.add(toolbar); at.setVisible(true); } public ArrowTest() { super(); this.getContentPane().setLayout(new GridBagLayout()); this.getContentPane().setBackground(Color.BLACK); this.setKeyBindings(); } public void setKeyBindings() { for (final Direction direction : Direction.values()) { MyArrowBinding binding = new MyArrowBinding(direction.getText()); getRootPane().registerKeyboardAction(binding, direction.getText(), direction.getKeyStroke(), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); } } enum Direction { UP("Up", KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0)), DOWN("Down", KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0)), LEFT("Left", KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0)), RIGHT("Right", KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0)); Direction(final String text, final KeyStroke keyStroke) { this.text = text; this.keyStroke = keyStroke; } private String text; private KeyStroke keyStroke; public String getText() { return text; } public KeyStroke getKeyStroke() { return keyStroke; } @Override public String toString() { return text; } } private class MyArrowBinding extends AbstractAction { private static final long serialVersionUID = -6904517741228319299L; public MyArrowBinding(final String text) { super(text); putValue(ACTION_COMMAND_KEY, text); } @Override public void actionPerformed(final ActionEvent e) { final String actionCommand = e.getActionCommand(); System.out.println("Key Binding: " + actionCommand); } } } </code></pre> <p>I also replaced your calls to <code>getActionMap/getInputMap</code> by a single call to <code>javax.swing.JComponent.registerKeyboardAction(ActionListener, String, KeyStroke, int)</code></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload