Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Event when window/stage lost focus

How can I run a piece of code (or more exactly: close the stage) when the JavaFX stage lost it's focus?

For example in Dropbox or Chrome: if you click the tray icon, a small window opens. If you click anywhere on the screen now, the window closes. Exactly this is the behaviour I want to create in my JavaFX application.

I searched a long time already for a solution, but couldn't find one...
So, I'm looking for something something like this:

stage.addEventHandler(EventType.FOCUS_LOST, new EventHandler() { /*...*/ } );


Thank you for helping me out!

like image 812
momar Avatar asked Jun 04 '14 13:06

momar


1 Answers

Add a listener to stage.focusedProperty().

primaryStage.focusedProperty().addListener(new ChangeListener<Boolean>()
{
  @Override
  public void changed(ObservableValue<? extends Boolean> ov, Boolean onHidden, Boolean onShown)
  {
    <Your code here>
  }
});
like image 151
OttPrime Avatar answered Oct 28 '22 13:10

OttPrime