Checking if window has keyboard focus again
Preface
I'm designing an application which will at one stage have it's keyboard focus taken away and redirected to another application. The application in question is Google-Chrome. At this stage my program should pause, or halt it's operations entirely until keyboard focus has been returned.
Some extra information:
- My application is being written in Java
- The application that is granted Keyboard focus is Google-Chrome
What I need
A way to test if a JFrame has keyboard focus maybe like:
JFrame.hasScreenFocus()
or something to that effect.
Answers
You should be able to use a WindowListener and listen for the windowActivated() event. You would also probably need to listen for windowDeactivated() to stop your current processing.
Make any other pop-up a modal dialog and it becomes a very simple question. The focus will return to the parent window the code line after the line which calls dialog.setVisible(true).
See also The Use of Multiple JFrames, Good/Bad Practice?