Close a window using a button

steyf65 -  
greg6614 Posted messages 629 Status Membre -
Hello,

I am new to programming in JAVA and I am using the javaFX tool and scenebuilder for a project. My project aims to display multiple images in a window, and when an image is clicked, a new window opens with a description...
I have successfully created the first window with the images and also the second window with a description for one of the images, but I cannot close this window using a button that I created.
Here is the code in the controller part:
None of my attempts to close this window work...
Thank you in advance for your help!

public class MainController {

@FXML
private Button exit;
Stage primaryStage = new Stage();

public void ImgAction(MouseEvent Event)throws Exception{
Stage primaryStage = new Stage();
Parent root = FXMLLoader.load(getClass().getResource("/application/Produit1.fxml"));
Scene scene = new Scene(root,800,480);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
//1st method
// if (exit.getOnAction()== null){
// primaryStage.close();
// }
//2nd method
// exit.setOnAction(e -> primaryStage.close());
}
//3rd method
// public void gestionBoutonCancel(ActionEvent event) {
// System.out.println("ok");
// primaryStage.close();

Configuration: Windows / Chrome 49.0.2623.112

1 réponse

greg6614 Posted messages 629 Status Membre 107
 
Hello,
You can use the method
dispose();


--
1