Yulfy
Yulfy

Reputation: 395

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:

What I need

A way to test if a JFrame has keyboard focus maybe like:

JFrame.hasScreenFocus()

or something to that effect.

Upvotes: 0

Views: 696

Answers (2)

camickr
camickr

Reputation: 324108

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.

Upvotes: 2

Andrew Thompson
Andrew Thompson

Reputation: 168825

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?

Upvotes: 2

Related Questions