Event when window/stage lost focus

青春壹個敷衍的年華 提交于 2019-12-06 21:00:08

问题


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!


回答1:


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>
  }
});


来源:https://stackoverflow.com/questions/24038988/event-when-window-stage-lost-focus

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!