Obtaining focus on a JPanel

39,685

Solution 1

Although you're indicating that the panel can be focusable, the panel isn't asking for focus. Try using myPanel.requestFocus();.

Solution 2

Use setFocusable(true) and then requestFocusInWindow(). But the latter must be done after the window containing the panel is made visible, for which you will likely need to register a window listener and do the requestFocusInWindow() in the window activated handler code.

Note: Specifically after the window is visible, not just after calling setVisible(true).

Solution 3

I sometimes face a similar problem. I've noticed that in some cases it is better to make or request focus on a specific control within the panel that is within the frame (e.g., the input box to which you want keyboard input to go), rather than request focus for the pane itself.

Solution 4

Try

panel.setFocusable(true);
panel.setRequestFocusEnabled(true);

// some code here

panel.grabFocus();

Solution 5

Try something like this:

    myFrame.addFocusListener(new FocusAdapter() {

        /**
         * {@inheritDoc}
         */
        @Override
        public void focusGained(FocusEvent aE) {
            myPanel.requestFocusInWindow();
        }
    });
Share:
39,685
Vlad T.
Author by

Vlad T.

Updated on December 15, 2020

Comments

  • Vlad T.
    Vlad T. over 3 years

    I have a JPanel inside a JFrame. I have registered a KeyListener, based on which I want to update the JPanel. The problem I am having is that I cannot get the focus on the JPanel and therefore my KeyListener won't work. I already know that the KeyListener is functional because I registered it with the JFrame and it worked fine. My code goes something like this at the moment:

    myFrame.setFocusable(false);
    myPanel.setFocusable(true);
    myPanel.addKeyListener(myKL);
    myFrame.add(myPanel);
    

    Has anyone encountered a problem like this before? Is there something I am missing in regards to this?

    P.S.: I do not have any components inside the JPanel I just draw an Image on the background, so I need the focus to be on the JPanel itself and not on something inside it.

  • Vlad T.
    Vlad T. over 14 years
    Thanks for your answer. Tried both requestFocus() and requestFocusInWindow(), neither of them make the Panel get focus. Do you have any other suggestions?
  • David Koelle
    David Koelle over 14 years
    It may depend on when you call it. Don't call it in the JPanel's constructor, for example, since that gets called before the panel is displayed.
  • Vlad T.
    Vlad T. over 14 years
    Thank you David, this answer worked for me, I had no idea that I couldn't make the call from the constructor. As soon as I moved the requestFocus() to a method that was getting called after the display on the screen it worked perfectly.
  • Gavin S. Yancey
    Gavin S. Yancey almost 5 years
    So how can my code tell when the window is visible, if waiting until after setVisible(true) is called is insufficient?
  • notTypecast
    notTypecast over 4 years
    For me, it worked when I called panel.requestFocusInWindow() after frame.pack().
  • Dreamspace President
    Dreamspace President over 3 years
    Generally, wrapping Swing commands into a SwingUtilities.invokeLater(()->{...});, even if you're already on the Swing thread, can solve such problems, because (Docs say) "This will happen after all pending AWT events have been processed." For example: If you want to protect Swing event code from causing its own execution, you can set a boolean at the beginning, and at the end you reset the boolean, but the latter has to be in an invokeLater(), else the reset is too early in the event sequence.
  • Brice
    Brice over 2 years
    For me I just needed to add this line on a JScrollPane to navigate with default key bindings. public void mouseEntered(MouseEvent e) { e.getComponent().requestFocus(); }.