如何触发内部关闭请求?
How can I fire internal close request?
我在 JavaFX 中关闭 window 时遇到问题。
我根据需要定义 setOnCloseRequest
,当我单击 window 中的 x 时它会起作用。但是,我还需要一个按钮来关闭 window 而这个 onCloseRequest
必须起作用,问题是它不起作用。该事件根本不会触发。
我正在使用 JavaFX 2.2 (Java 7),我注意到 setOnCloseRequest
的参考资料说在 external 上关闭 window请求
解决方案
从您的内部关闭请求(在按下按钮时)触发一个事件,以便应用程序认为它收到了外部关闭请求。那么无论请求来自外部事件还是内部事件,您的关闭请求逻辑都可以相同。
private EventHandler<WindowEvent> confirmCloseEventHandler = event -> {
// close event handling logic.
// consume the event if you wish to cancel the close operation.
}
...
stage.setOnCloseRequest(confirmCloseEventHandler);
Button closeButton = new Button("Close Application");
closeButton.setOnAction(event ->
stage.fireEvent(
new WindowEvent(
stage,
WindowEvent.WINDOW_CLOSE_REQUEST
)
)
);
备注
这是一个Java 8+的解决方案,对于JavaFX 2,你将需要在匿名内部转换lambda函数类并且将无法使用Alert对话框,但需要提供您自己的警报对话框系统,因为 JavaFX 2 没有内置的系统。我强烈建议升级到 Java 8+ 而不是继续使用 JavaFX 2.
样本UI
示例代码
示例代码将向用户显示关闭确认警报,如果用户未确认关闭,则取消关闭请求。
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.StackPane;
import javafx.stage.*;
import javafx.stage.WindowEvent;
import java.util.Optional;
public class CloseConfirm extends Application {
private Stage mainStage;
@Override
public void start(Stage stage) throws Exception {
this.mainStage = stage;
stage.setOnCloseRequest(confirmCloseEventHandler);
Button closeButton = new Button("Close Application");
closeButton.setOnAction(event ->
stage.fireEvent(
new WindowEvent(
stage,
WindowEvent.WINDOW_CLOSE_REQUEST
)
)
);
StackPane layout = new StackPane(closeButton);
layout.setPadding(new Insets(10));
stage.setScene(new Scene(layout));
stage.show();
}
private EventHandler<WindowEvent> confirmCloseEventHandler = event -> {
Alert closeConfirmation = new Alert(
Alert.AlertType.CONFIRMATION,
"Are you sure you want to exit?"
);
Button exitButton = (Button) closeConfirmation.getDialogPane().lookupButton(
ButtonType.OK
);
exitButton.setText("Exit");
closeConfirmation.setHeaderText("Confirm Exit");
closeConfirmation.initModality(Modality.APPLICATION_MODAL);
closeConfirmation.initOwner(mainStage);
// normally, you would just use the default alert positioning,
// but for this simple sample the main stage is small,
// so explicitly position the alert so that the main window can still be seen.
closeConfirmation.setX(mainStage.getX());
closeConfirmation.setY(mainStage.getY() + mainStage.getHeight());
Optional<ButtonType> closeResponse = closeConfirmation.showAndWait();
if (!ButtonType.OK.equals(closeResponse.get())) {
event.consume();
}
};
public static void main(String[] args) {
launch(args);
}
}
我在 JavaFX 中关闭 window 时遇到问题。
我根据需要定义 setOnCloseRequest
,当我单击 window 中的 x 时它会起作用。但是,我还需要一个按钮来关闭 window 而这个 onCloseRequest
必须起作用,问题是它不起作用。该事件根本不会触发。
我正在使用 JavaFX 2.2 (Java 7),我注意到 setOnCloseRequest
的参考资料说在 external 上关闭 window请求
解决方案
从您的内部关闭请求(在按下按钮时)触发一个事件,以便应用程序认为它收到了外部关闭请求。那么无论请求来自外部事件还是内部事件,您的关闭请求逻辑都可以相同。
private EventHandler<WindowEvent> confirmCloseEventHandler = event -> {
// close event handling logic.
// consume the event if you wish to cancel the close operation.
}
...
stage.setOnCloseRequest(confirmCloseEventHandler);
Button closeButton = new Button("Close Application");
closeButton.setOnAction(event ->
stage.fireEvent(
new WindowEvent(
stage,
WindowEvent.WINDOW_CLOSE_REQUEST
)
)
);
备注
这是一个Java 8+的解决方案,对于JavaFX 2,你将需要在匿名内部转换lambda函数类并且将无法使用Alert对话框,但需要提供您自己的警报对话框系统,因为 JavaFX 2 没有内置的系统。我强烈建议升级到 Java 8+ 而不是继续使用 JavaFX 2.
样本UI
示例代码
示例代码将向用户显示关闭确认警报,如果用户未确认关闭,则取消关闭请求。
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.StackPane;
import javafx.stage.*;
import javafx.stage.WindowEvent;
import java.util.Optional;
public class CloseConfirm extends Application {
private Stage mainStage;
@Override
public void start(Stage stage) throws Exception {
this.mainStage = stage;
stage.setOnCloseRequest(confirmCloseEventHandler);
Button closeButton = new Button("Close Application");
closeButton.setOnAction(event ->
stage.fireEvent(
new WindowEvent(
stage,
WindowEvent.WINDOW_CLOSE_REQUEST
)
)
);
StackPane layout = new StackPane(closeButton);
layout.setPadding(new Insets(10));
stage.setScene(new Scene(layout));
stage.show();
}
private EventHandler<WindowEvent> confirmCloseEventHandler = event -> {
Alert closeConfirmation = new Alert(
Alert.AlertType.CONFIRMATION,
"Are you sure you want to exit?"
);
Button exitButton = (Button) closeConfirmation.getDialogPane().lookupButton(
ButtonType.OK
);
exitButton.setText("Exit");
closeConfirmation.setHeaderText("Confirm Exit");
closeConfirmation.initModality(Modality.APPLICATION_MODAL);
closeConfirmation.initOwner(mainStage);
// normally, you would just use the default alert positioning,
// but for this simple sample the main stage is small,
// so explicitly position the alert so that the main window can still be seen.
closeConfirmation.setX(mainStage.getX());
closeConfirmation.setY(mainStage.getY() + mainStage.getHeight());
Optional<ButtonType> closeResponse = closeConfirmation.showAndWait();
if (!ButtonType.OK.equals(closeResponse.get())) {
event.consume();
}
};
public static void main(String[] args) {
launch(args);
}
}