This site requires JavaScript, please enable it in your browser!
Greenfoot back
JohnDoeWasTaken
JohnDoeWasTaken wrote ...

2019/4/28

focusedProperty not updating

JohnDoeWasTaken JohnDoeWasTaken

2019/4/28

#
I would like to stop the game when the exported game's window loses focus. I tried to add a listener to the window's focused property, but it does not seem to fire. Note: I have confirmed that the window variable is not null when run in an exported game. The code to stop the game when focus is lost is still not working, though.
Window window = ripper.getWindow();
if (window == null)
    return;
ChangeListener<Boolean> listener = (observable, oldValue, newValue) ->
{
    if (!newValue)
        Greenfoot.stop();
};
window.focusedProperty().addListener(listener);
The getWindow() method looks as follows:
WorldHandlerDelegateStandAlone delegate = getStandaloneDelegate();
if (delegate == null)
    return null;
GreenfootScenarioViewer viewer = getField("viewer", delegate);
WorldDisplay world = getField("worldDisplay", viewer);
ImageView imageView = (ImageView)world.getImageView();
return imageView.getScene().getWindow();
The getField method simply retrieves the specified field's value using reflection. Any help is appreciated.
nccb nccb

2019/4/29

#
This is a hack that we don't officially support... but having said that, the code looks like it should work. Technically you should add the focus listener on the JavaFX thread but I doubt that is the issue. If you run the exported program from the command line, do you see any exceptions in the console? Maybe add some System.out.println lines to check which parts of the code are reached.
You need to login to post a reply.