Ranjan
Ranjan

Reputation: 328

How to check if the current Java application is in foreground on my Windows PC?

I want to check if my swing application is in the the foreground. If that's not the case I would use the application's system tray icon to display balloon notifications for some specific events. Any ideas on how to check that?

Upvotes: 3

Views: 3245

Answers (2)

Dinesh
Dinesh

Reputation: 124

Focus subsystem helps you to check whether window is being active or not.

jframe.addWindowFocusListener(new WindowAdapter() {

    //To check window gained focus
    public void windowGainedFocus(WindowEvent e) {
        //set flag
        isWindowActive = true;
    }

    //To check window lost focus
    public void windowLostFocus(WindowEvent e) {
        //set flag
        isWindowActive = false;
    }
});

HTH

Upvotes: 1

Neet
Neet

Reputation: 4057

isActive() and isFocused() are a good starting point so see whether your frame has focus or not.

WindowListener may also be used to react on windowActivated and windowDeactivated.

Upvotes: 4

Related Questions