Search code examples
javalibgdx

LibGdx close Window


I've one problem with LibGdx. I have a "MainGameScreen" class and a coustom Window(scene2d Ui) class. I have a "TextButton" in my Window. What is the best way to close the window when the button is pressed ? Thanks !


Solution

  • What you want to do is add a ChangeListener to the button which closes the window whenever the button is pressed.

    Here is a small demo of how you could do it:

    // The window has to be final to be accessible from our listener.
    final Window window = new Window("Title", skin);
    
    // Create our button.
    TextButton button = new TextButton("Press me to close window!", skin);
    
    // Here we add a click listener to our button.
    button.addListener (new ChangeListener() {
        // This method is called whenever the actor is clicked. We override its behavior here.
        @Override
        public void changed(ChangeEvent event, Actor actor) {
            // This is where we remove the window.
            window.remove();
        }
    });
    
    // Add the button to our window.
    window.add(button);
    
    // Add the window to our stage.
    stage.addActor(window);