component in fullscreen

i add several swing component in fullscreen mode
it works nicely when i put non-container swing component
like JButton, JTextfield, JSlider…
but when i put container like JPanel, all keyboard input doesn’t work when i click some component in that container…
i can’t move the character or quit (by pressing ESC)

the problem is the main frame (JFrame) is lost the focus, and the container got the focus
if i request the focus back to main frame, it works again
mainFrame.requestFocus(); :slight_smile:

so can the mainFrame still have the focus when the container got the focus too?
or what method should i use, so when i click the mainFrame it will gain the focus again?
i addKeyListener and addMouseListener to the mainFrame (JFrame)
i try to add the listener to its contentPane but it getting worse, not working at all. >:(

I realize this is an old topic, but I figured I’d reply anyways in case someone looks at this topic when searching (like I did).

Extend JFrame (I actually did this with JPanel instead of JFrame) with a custom class. In the custom class, have a “setKeyListener” method or have a KeyListener get taken as an argument by the constructor. Store this KeyListener. Then have a method for adding Components something like the following:

public void add(Component component) {
super.add(component);
if(myKeyListener != null)
component.addKeyListener(myKeyListener);
}

This adds the same KeyListener to every Component in the Container. This makes it a moot point which Component has the focus. This doesn’t work well with JTextFields, etc. If the ‘a’ key does something in your game and the JTextField has focus, ‘a’ will be typed in the JTextField and have whatever effect it has in the game. You could, of course, make some sort of workaround - either only adding the KeyListener to some Components or removing it from some Components after it’s added.