按代码关闭fxml窗口,javafx

时间:2022-04-07 23:38:51

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

我需要通过控制器中的代码关闭当前的fxml窗口

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

我知道stage.close()或stage.hide()在fx中执行此操作

how to implement this in fxml? I tried

如何在fxml中实现这个?我试过了

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!

非常感谢所有帮助。谢谢!

7 个解决方案

#1


112  

  1. give your close button an fx:id, if you haven't yet: <Button fx:id="closeButton" onAction="#closeButtonAction">
  2. 给你的关闭按钮一个fx:id,如果你还没有:

  3. 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


16  

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

如果你有一个扩展javafx.application.Application的窗口;您可以使用以下方法。

Platform.exit();

Example:

public class MainGUI extends Application {
.........

Button exitButton = new Button("Exit");
exitButton.setOnAction(new ExitButtonListener());
.........

public class ExitButtonListener implements EventHandler<ActionEvent> {

  @Override
  public void handle(ActionEvent arg0) {
    Platform.exit();
  }
}

Edit for the beauty of Java 8:

编辑Java 8之美:

 public class MainGUI extends Application {
    .........

    Button exitButton = new Button("Exit");
    exitButton.setOnAction(actionEvent -> Platform.exit());
 }

#3


4  

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

我从接受的答案中收到NullPointerException后,以下面的方式实现了这个。

In my FXML:

在我的FXML中:

<Button onMouseClicked="#onMouseClickedCancelBtn" text="Cancel">

In my Controller class:

在我的Controller类中:

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

#4


1  

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)

(假设您的控制器是节点。否则您必须先获取节点(getScene()是Node的方法)

#5


1  

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


0  

you can simply achieve this with,

你可以简单地实现这一点,

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

will do the trick for you.

会为你做的伎俩。

#7


0  

stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
    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.

它相当于隐藏。因此,当您下次打开它时,您只需检查阶段对象是否已退出。如果退出,则只显示()ie(stage.show())调用。否则,你必须开始阶段。

#1


112  

  1. give your close button an fx:id, if you haven't yet: <Button fx:id="closeButton" onAction="#closeButtonAction">
  2. 给你的关闭按钮一个fx:id,如果你还没有:

  3. 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


16  

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

如果你有一个扩展javafx.application.Application的窗口;您可以使用以下方法。

Platform.exit();

Example:

public class MainGUI extends Application {
.........

Button exitButton = new Button("Exit");
exitButton.setOnAction(new ExitButtonListener());
.........

public class ExitButtonListener implements EventHandler<ActionEvent> {

  @Override
  public void handle(ActionEvent arg0) {
    Platform.exit();
  }
}

Edit for the beauty of Java 8:

编辑Java 8之美:

 public class MainGUI extends Application {
    .........

    Button exitButton = new Button("Exit");
    exitButton.setOnAction(actionEvent -> Platform.exit());
 }

#3


4  

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

我从接受的答案中收到NullPointerException后,以下面的方式实现了这个。

In my FXML:

在我的FXML中:

<Button onMouseClicked="#onMouseClickedCancelBtn" text="Cancel">

In my Controller class:

在我的Controller类中:

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

#4


1  

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)

(假设您的控制器是节点。否则您必须先获取节点(getScene()是Node的方法)

#5


1  

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


0  

you can simply achieve this with,

你可以简单地实现这一点,

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

will do the trick for you.

会为你做的伎俩。

#7


0  

stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
    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.

它相当于隐藏。因此,当您下次打开它时,您只需检查阶段对象是否已退出。如果退出,则只显示()ie(stage.show())调用。否则,你必须开始阶段。