Note that there are some explanatory texts on larger screens.

plurals
  1. POJava: JPanel doesn't pick up keyboard bindings
    primarykey
    data
    text
    <h1>Problem</h1> <p>I've been fiddling to make keybindings work properly in an application I've written. </p> <p>Previously, I've been using a variant of the following; <code>panel.registerKeyboardAction(this, "createNewFood", KeyStroke.getKeyStroke(KeyEvent.VK_I, KeyEvent.CTRL_DOWN_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);</code></p> <p>But since I read in the documentation that registerKeyboardAction was marked as deprecated, I tried switching to the preferred method, which goes something like this; <code>panel.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke("control I"), new NewFoodAction());</code></p> <p>Unfortunately this doesn't seem to be working. </p> <h1>What I've Tried</h1> <p>I've searched the web and I've tried a bunch of different approaches unsuccessfully; </p> <ul> <li>Instead of binding the key to the panel I tried attaching it to the result of <code>getRootPane()</code>. Didn't work. </li> <li>I've tried all of the different "conditions"; <code>WHEN_IN_FOCUSED_WINDOW</code>, <code>WHEN_ANCESTOR_OF_FOCUSED_COMPONENT</code>, <code>WHEN_FOCUSED</code>, didn't work.</li> <li>I tried setting <code>panel.setFocusable(true)</code>; didn't work.</li> <li>I tried using <code>panel.requestFocusInWindow()</code> just to see if it could work conditionally; didn't work.</li> </ul> <p>If I attach the keybinding to another component, for instance a JTextField, then it works as it's supposed to. </p> <p><strong>Some other information that <em>might</em> be relevant</strong> (but I don't really think it is); </p> <ul> <li>I'm using MigLayout for the panel. Don't think this affects anything but who knows. </li> <li>I have other keybindings present (that is, other keystrokes bound to other components)</li> </ul> <p>Here's some sample code: </p> <pre><code>public FoodFrame() { super("MealTrack"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setPreferredSize(new Dimension(1400, 600)); setLocation(300, 100); setVisible(true); panel = new JPanel(new MigLayout("fill", "[grow][]", "[][][][grow][][]")); add(panel); panel.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke("control I"), new NewFoodAction()); pack(); filter.requestFocusInWindow(); } private class NewFoodAction extends AbstractAction { @Override public void actionPerformed(ActionEvent e) { System.out.println("called"); } } </code></pre> <p>}</p> <p>Does anyone know what the problem seems to be?</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.
 

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