Obtaining focus on a JPanel
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();
}
});
Vlad T.
Updated on December 15, 2020Comments
-
Vlad T. over 3 years
I have a
JPanel
inside aJFrame
. I have registered aKeyListener
, based on which I want to update theJPanel
. The problem I am having is that I cannot get the focus on theJPanel
and therefore myKeyListener
won't work. I already know that theKeyListener
is functional because I registered it with theJFrame
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. over 14 yearsThanks for your answer. Tried both requestFocus() and requestFocusInWindow(), neither of them make the Panel get focus. Do you have any other suggestions?
-
David Koelle over 14 yearsIt 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. over 14 yearsThank 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 almost 5 yearsSo how can my code tell when the window is visible, if waiting until after
setVisible(true)
is called is insufficient? -
notTypecast over 4 yearsFor me, it worked when I called
panel.requestFocusInWindow()
afterframe.pack()
. -
Dreamspace President over 3 yearsGenerally, 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 over 2 yearsFor 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(); }
.