close fxml window by code, javafx

匿名 (未验证) 提交于 2019-12-03 01:05:01

问题:

I need to close the current fxml window by code in the controller

I know stage.close() or stage.hide() do this in fx

how to implement this in fxml? I tried

private void on_btnClose_clicked(ActionEvent actionEvent) {         Parent root = FXMLLoader.load(getClass().getResource("currentWindow.fxml"));             Scene scene = new Scene(root);          Stage stage = new Stage();                     stage.setScene(scene);         stage.show(); }

but it doesn't work!

All help will be greatly appreciated. Thanks!

回答1:

  1. give your close button an fx:id, if you haven't yet:
  2. In your controller class:

    @FXML private javafx.scene.control.Button closeButton;  @FXML private void closeButtonAction(){     // get a handle to the stage     Stage stage = (Stage) closeButton.getScene().getWindow();     // do what you have to do     stage.close(); }


回答2:

If you have a window which extends javafx.application.Application; you can use the following method.

Platform.exit();

Example:

public class MainGUI extends Application { .........  Button exitButton = new Button("Exit"); exitButton.setOnAction(new ExitButtonListener()); .........  public class ExitButtonListener implements EventHandler {    @Override   public void handle(ActionEvent arg0) {     Platform.exit();   } }

Edit for the beauty of Java 8:

 public class MainGUI extends Application {     .........      Button exitButton = new Button("Exit");     exitButton.setOnAction(actionEvent -> Platform.exit());  }


回答3:

I implemented this in the following way after receiving a NullPointerException from the accepted answer.

In my FXML:

In my Controller class:

@FXML public void onMouseClickedCancelBtn(InputEvent e) {     final Node source = (Node) e.getSource();     final Stage stage = (Stage) source.getScene().getWindow();     stage.close(); }


回答4:

I'm not sure if this is the best way (or if it works), but you could try:

private void on_btnClose_clicked(ActionEvent actionEvent) {          Window window = getScene().getWindow();             if (window instanceof Stage){             ((Stage) window).close();         } }

(Assuming your controller is a Node. Otherwise you have to get the node first (getScene() is a method of Node)



回答5:

I found a nice solution which does not need an event to be triggered:

@FXML private Button cancelButton;  close(new Event(cancelButton, stage, null));  @FXML private void close(Event event) {     ((Node)(event.getSource())).getScene().getWindow().hide();                       }


回答6:

stage.setOnCloseRequest(new EventHandler() {     public void handle(WindowEvent we) {                                 Platform.setImplicitExit(false)             stage.close()         }     });

It is equivalent to hide. So when you are going to open it next time, you just check if the stage object is exited or not. If it is exited, you just show() i.e. (stage.show()) call. Otherwise, you have to start the stage.



回答7:

you can simply achieve this with,

@FXML private void closeAction(ActionEvent evt){     System.exit(0); }

will do the trick for you.



转载请标明出处:close fxml window by code, javafx
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!