0

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.

Yulfy
  • 395
  • 1
  • 4
  • 16

2 Answers2

2

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?

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Thanks for the answer. I don't think I was clear enough in my post though, I'm not passing keyboard focus to another Window created within my own program. It is a window on a separate program that will grab keyboard focus and I need to wait until it has been returned to continue the program. If this helps clarify my question I'll update the OP. – Yulfy May 14 '13 at 15:13
2

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.

camickr
  • 321,443
  • 19
  • 166
  • 288