1
|
import com.sun.javafx.stage.StageHelper;
|
2
|
|
3
|
import javafx.geometry.Rectangle2D;
|
4
|
import javafx.scene.control.Alert;
|
5
|
import javafx.scene.control.Alert.AlertType;
|
6
|
import javafx.stage.Screen;
|
7
|
import javafx.stage.Window;
|
8
|
|
9
|
public class Report {
|
10
|
|
11
|
private static final int HEIGHT = 200;
|
12
|
private static final int WIDTH = 500;
|
13
|
|
14
|
private static void setPosition(Alert alert) {
|
15
|
// For Java 8.
|
16
|
if (StageHelper.getStages() != null && StageHelper.getStages().size() != 0) {
|
17
|
if (Screen.getPrimary() != null && Screen.getPrimary().getVisualBounds() != null) {
|
18
|
Rectangle2D primScreenBounds = Screen.getPrimary().getVisualBounds();
|
19
|
alert.setX((primScreenBounds.getWidth() - WIDTH - 20) / 2);
|
20
|
alert.setY((primScreenBounds.getHeight() - HEIGHT - 20) / 3);
|
21
|
}
|
22
|
alert.initOwner(StageHelper.getStages().get(StageHelper.getStages().size() - 1));
|
23
|
}
|
24
|
/* For Java 11.
|
25
|
if (Window.getWindows() != null && Window.getWindows().size() != 0) {
|
26
|
if (Screen.getPrimary() != null && Screen.getPrimary().getVisualBounds() != null) {
|
27
|
Rectangle2D primScreenBounds = Screen.getPrimary().getVisualBounds();
|
28
|
alert.setX((primScreenBounds.getWidth() - WIDTH - 20) / 2);
|
29
|
alert.setY((primScreenBounds.getHeight() - HEIGHT - 20) / 3);
|
30
|
}
|
31
|
alert.initOwner(Window.getWindows().get(Window.getWindows().size() - 1));
|
32
|
}*/
|
33
|
}
|
34
|
|
35
|
private static Alert createAlert(AlertType type, String title, String header, String content) {
|
36
|
Alert alert = new Alert(type);
|
37
|
alert.setResizable(true);
|
38
|
alert.getDialogPane().setPrefWidth(WIDTH);
|
39
|
alert.setTitle(title);
|
40
|
alert.setHeaderText(header);
|
41
|
alert.setContentText(content);
|
42
|
setPosition(alert);
|
43
|
return alert;
|
44
|
}
|
45
|
|
46
|
public static void error(String title, String header, String content) {
|
47
|
createAlert(AlertType.WARNING, title, header, content).showAndWait();
|
48
|
}
|
49
|
|
50
|
public static void info(String title, String header, String content) {
|
51
|
createAlert(AlertType.INFORMATION, title, header, content).showAndWait();
|
52
|
}
|
53
|
|
54
|
public static boolean confirm(String title, String header, String content) {
|
55
|
Alert alert = createAlert(AlertType.CONFIRMATION, title, header, content);
|
56
|
alert.showAndWait();
|
57
|
if (alert.getResult().getText().equals("OK")) {
|
58
|
return true;
|
59
|
} else {
|
60
|
return false;
|
61
|
}
|
62
|
}
|
63
|
|
64
|
}
|