Correct way to capture (and ignore) an exit [X] button press

I’m looking to exit gracefully when the X button is clicked on a JMonkey window. In my case this means:

  • Display a progress bar saying “saving”
  • Save the current game
  • Exit

I’m not just looking to do a cleanup phase as described in Close Application when Exit.

I have a solution to this but it feels very much like the wrong way to do it:

//within SimpleApplication
/**
 * If we ignore the close request we'll just keep on being asked, so we ignore all but the first and close when the save has finished
 */
boolean closeActivated = false;
@Override
public void requestClose (boolean esc) {
    if (!closeActivated) {
        closeActivated = true;
        guiStack.addFrame(SimpleProgress.formScreen("Saving"));
        WorldSaveAndExit.saveAndExitAsynchronous(world, () -> {
            super.requestClose(esc);
        } );
    }
}

This of course really needs to end up closing soon because requestClose keeps getting called, but if I for example wanted an “are you sure” dialog then it might need to abort the close.

What is the right way to accept the UI event of the X button being clicked and block it from actually starting the shutdown

The reason the method is called over and over is because JME is just checking a flag provided by lwjgl (either version). While there are differences between lwjgl2 and lwjgl3 how this state is dealt with, in this case it is the same. JME does not provide a way to clear this flag and will not clear it on its own. (I imagine on Android that it’s maybe impossible to abort anyway.)

As such, unless someone proposes a patch to add something to JME’s API, one cannot abort closing the application on “X” or Altf-F4, only delay it (as you’ve discovered).

Beyond that, if you kept running and just ignoring the notifications then you’d never know when the user really tried to close the application again later.

1 Like

Sounds like this is the “least worst” way of doing this then. Thanks

But the real question is: why do you want to save on hard exits like ‘X’ or Alt-F4?

Hide them, catch the input yourself and manage it more cleanly.

Perhaps I don’t understand the question. When jmonkey isnt in full screen it shows the X button.

What do you mean by hide them?

I want a behaviour like for example Microsoft Word where that X-ing out triggers a dialog

The request is reasonable. Sometimes JME misses things like this because 99.999% of games won’t really need it. The fact that we write game editors with it, too, means that sometimes we DO need it.

Even in my own editors, if the user hits the X they are screwed if they didn’t save or wanted to keep working without saving. (Because most of my tools are not released in the wild yet, I consider it a feature that I can easily hard-abort the normal shutdown checks versus hitting ‘esc’ which has all of the nice checks… but a user would be very sad if they messed up after making a bunch of edits.)

1 Like

if the user hits the X they are screwed if they didn’t save

Same with Maud, except Maud is released in the wild. The best workaround I’ve found is to run the editor in full-screen mode: there’s no X-button visible, but Alt-F4 or a SIGTERM signal is still likely to prove fatal.

With Alt-F4 you probably know what you asked for though

I think it’s possible to have a windowed screen that DON’T show the X

But I understand that you really want a MS Office like interface, so you won’t like my suggestion.