-
Notifications
You must be signed in to change notification settings - Fork 1
/
GracefullExit.java
50 lines (36 loc) · 1.1 KB
/
GracefullExit.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package sample;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class GracefullExit extends Application {
Stage window;
public static void main(String[] args) {
launch(args);
}
public void start(Stage stage) {
window = stage;
window.setTitle("This is close example");
//Graceful close from close sign on window
window.setOnCloseRequest(e -> {
e.consume(); //Consuming the event to avoid window close on chosing no
closeProgram();
});
Button button = new Button();
button.setText("Close");
//Graceful close from button
button.setOnAction(e -> closeProgram());
StackPane root = new StackPane();
root.getChildren().addAll(button);
Scene scene = new Scene(root, 300, 300);
window.setScene(scene);
window.show();
}
private void closeProgram() {
System.out.println("Saving the file!");
Boolean result = ConfirmBox.display("Exiting", "Are you sure ?");
if (result)
window.close();
}
}