Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to close a java window with a button click - JavaFX Project

Tags:

java

javafx

I made a JavaFX project and created the GUI for the first-login frame in the java Scene Builder. When the login is successful, the login frame must be closed and the next frame must be visible (main program frame). I can make the new frame appear, but I can't make the login frame closed. I tried stuff like dispose() but nothing works. Below is the code for the main class:

public class KuberComm extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));

        Scene scene = new Scene(root);
        stage.setResizable(false);
        stage.setTitle("Login to KuberComm");
        stage.setScene(scene);

        stage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}

The handler for the login button is located in another class (controller class made by the NetBeans IDE). I can't figure out what the frame's name is in order to use it in the controller class.

Any help will be much appreciated!

like image 306
Aristomenis Avatar asked Jul 30 '14 13:07

Aristomenis


People also ask

How do I close a JavaFX program?

stop() method and terminate the JavaFX application thread.

How do I close FXML?

@ZinMinn if you want to close the app, just call Platform. exit().

Do something when button is pressed JavaFX?

To make a button do something in JavaFX you need to define the executable code usually by using a convenience method like setOnAction() . Then, when the button is clicked, JavaFX does the heavy lifting of fetching that pre-defined code, and executing it on the JavaFX Application thread.


3 Answers

give your button a name in the controller class:

@FXML
public Button closeButton;

and add this method:

@FXML
public void handleCloseButtonAction(ActionEvent event) {
    Stage stage = (Stage) closeButton.getScene().getWindow();
    stage.close();
}

In your FXML you need a reference to the button name and the method to call onAction:

<Button fx:id="closeButton" cancelButton="true" layoutX="350.0" layoutY="767.0" mnemonicParsing="false" onAction="#handleCloseButtonAction" prefWidth="100.0" text="Close" />

This would close the stage that this button is on.

like image 158
Moh-Aw Avatar answered Oct 19 '22 15:10

Moh-Aw


Similar to the other answers but more precise.

@FXML
public void handleCloseButtonAction(ActionEvent event) {
    ((Stage)(((Button)event.getSource()).getScene().getWindow())).close();
}
like image 38
Sedrick Avatar answered Oct 19 '22 14:10

Sedrick


Use

stage.hide()

If you do this from a controller, you can get the stage from any Node inside the scene of the stage (if necessary let the FXML loader assign one to a field of the controller using the id attribute from the fxml namespace in the fxml):

Window stage = node.getScene().getWindow();
like image 8
fabian Avatar answered Oct 19 '22 14:10

fabian