I want to pause my game (written in Java Swing), when the player switches to another application.

But I did not find a way to detect this switch. window.isActive() only works for multiple windows within one Java application. But it does return true, when a different application has the focus.

The same is true for the KeyboardFocusManager, it returns the window of my application that had the focus before the focus was given to another application.

  • 1
    \$\begingroup\$ While you may be making a game, this issue is not solely related to game development. "General programming questions more likely belong on Stack Overflow instead of here. A good rule of thumb is to ask yourself... Would a professional game developer give me a better/different/more specific answer to this question than other programmers?" I think this question fails that test. \$\endgroup\$
    – House
    Commented Jul 16, 2013 at 17:08

1 Answer 1


Use the focus subsystem, as documented here.

An example of detecting focus being gained:

//Make textField get the focus whenever frame is activated.
frame.addWindowFocusListener(new WindowAdapter() {
    public void windowGainedFocus(WindowEvent e) {

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .