views:

110

answers:

1

Hello,

In my Swing application, I'm using a custom module to record a screen cast.

However, I'm a bit hung up on when to force the recording to stop. Right now, I check to see if the user still wishes to record (meaning that they haven't clicked the 'stop' button) and if the application is still open (closing the application causes the recording to stop gracefully).

The problem is that the recording will continue if the application gets pushed behind other apps or minimized, causing recording of 'junk'. I need the recording to stop when the application is no longer 'on top' of the screen. Using the application's focus doesn't seem to work due to other dialogs and things popping up.

Suggestions?

+3  A: 

You may want to try adding a WindowListener and override the windowDeactivated() event, which should get called when the frame is no longer the "active window" according to the operating system.

UPDATE:

If you are conscientious about making sure that your child dialogs and windows are owned by your application (making sure you pass in your application frame as the owner), then you could make your WindowListener do something like this:

 public void windowDeactivated(WindowEvent e) {
  if(e.getOppositeWindow() == null){
   // will be null when another application window gets activated, stop recording
  }
  for(Window w : appFrame.getOwnedWindows()){
   if(w.equals(e.getOppositeWindow())){
    // one of the windows owned by the frame is active, don't stop recording
   }
  }

 }

Then you will be able to determine if the window focus has left your application altogether or if the focus has just changed to a different child window/dialog.

BryanD
The problem with this approach is that when the application launches a child JDialog, the application is no longer the active window, causing my screen cast to stop. I need to check for active on the application and its dialogs.
thedude19
Oh, I see .. so something like an "application focus loss" event.
BryanD
Exactly. Is that possible?
thedude19
Yes! This is exactly what I just came up with right before you updated and it works beautifully. Thank you! (referring to your update)
thedude19