Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8065509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:41:13+00:00 2026-06-05T11:41:13+00:00

Problem I’ve been fiddling to make keybindings work properly in an application I’ve written.

  • 0

Problem

I’ve been fiddling to make keybindings work properly in an application I’ve written.

Previously, I’ve been using a variant of the following;
panel.registerKeyboardAction(this, "createNewFood", KeyStroke.getKeyStroke(KeyEvent.VK_I, KeyEvent.CTRL_DOWN_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);

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;
panel.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke("control I"), new NewFoodAction());

Unfortunately this doesn’t seem to be working.

What I’ve Tried

I’ve searched the web and I’ve tried a bunch of different approaches unsuccessfully;

  • Instead of binding the key to the panel I tried attaching it to the result of getRootPane(). Didn’t work.
  • I’ve tried all of the different “conditions”; WHEN_IN_FOCUSED_WINDOW, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, didn’t work.
  • I tried setting panel.setFocusable(true); didn’t work.
  • I tried using panel.requestFocusInWindow() just to see if it could work conditionally; didn’t work.

If I attach the keybinding to another component, for instance a JTextField, then it works as it’s supposed to.

Some other information that might be relevant (but I don’t really think it is);

  • I’m using MigLayout for the panel. Don’t think this affects anything but who knows.
  • I have other keybindings present (that is, other keystrokes bound to other components)

Here’s some sample 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");
    }

}

}

Does anyone know what the problem seems to be?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T11:41:15+00:00Added an answer on June 5, 2026 at 11:41 am

    You are doing it wrong. You need to use both ActionMap and InputMap. You should do:

    panel.getInputMap(con).put(KeyStroke.getKeyStroke("control I"), "createNewFood");
    panel.getActionMap().put("createNewFood", new NewFoodAction());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Problem I have a strange problem. I have written an application in MFC(using VS2003)
Problem: I've written State Machine for my android application. It is separate class, extension
Problem I have a Silverlight 5 application using the treeview from the SDK. Now
Problem: How to make Meta-x work in emacs? Hi I am a newcomer to
Problem: Been struggling to get my code to load external shaders and it is
Problem: I am trying to build a recursive tree using a function and data
Problem: I'm streaming my video from a php file with stream_get_contents(); using Flowplayer as
Problem Using Director 11.5 and Windows 7, with MouseWheel Xtra (wheelmouse.zip), I have the
Problem occured when i tried to display xml data that has been taken by
Problem! I Have the following input (rules) from a flat file (talking about numeric

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.