Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get current active window while running my java application

Tags:

java

swing

I need to get current active window. I have used KeyboardFocusManager, for getting active window. But i am getting active window is null. below is the code. please provide any way to get current active window.

KeyboardFocusManager currentManager = KeyboardFocusManager.getCurrentKeyboardFocusManager();  
Window activeWindow = currentManager.getActiveWindow();
like image 553
sravani reddy Avatar asked Dec 25 '22 10:12

sravani reddy


1 Answers

This single line of code should work:

Window activeWindow = javax.swing.FocusManager.getCurrentManager().getActiveWindow();

from the JavaDoc:

Returns the active Window, if the active Window is in the same context as the calling thread. Only a Frame or a Dialog can be the active Window. The native windowing system may denote the active Window or its children with special decorations, such as a highlighted title bar. The active Window is always either the focused Window, or the first Frame or Dialog that is an owner of the focused Window.

to get the GlobalActiveWindow, call:

 javax.swing.FocusManager.getCurrentManager().getGlobalActiveWindow();

JavaDoc:

Returns the active Window, even if the calling thread is in a different context than the active Window. Only a Frame or a Dialog can be the active Window. The native windowing system may denote the active Window or its children with special decorations, such as a highlighted title bar. The active Window is always either the focused Window, or the first Frame or Dialog that is an owner of the focused Window.

Note: When your application does not have the focus, this method returns null!

Cheers!

like image 166
Ben Avatar answered Jan 19 '23 00:01

Ben