I need to run few task when switching to my Java Swing application from other application. Let say, I have opened my application (TestApp) first and opened Notepad. Now both applications are opened and Notepad is focused. When I minimize Notepad or click on the TestApp icon in the taskbar or select the TestApp using Alt Tab, TestApp will become current application and gets focused. In this moment, I need to run some task. Is there any listener in Java Swing / JavaFx to listen to this application switch event?
Thanks in advance!!!
CodePudding user response:
You are looking for WindowFocusListener: https://docs.oracle.com/javase/8/docs/api/java/awt/event/WindowFocusListener.html
CodePudding user response:
Yes there is a listener exactly for that case. WindowListener provides the method windowActivated(WindowEvent) which gets called every time the window gets activated (gets focused).
Simply implement this interface and implement the method to use it.