I am trying to implement some animation in my project. When a user uses the application, sometimes he or she receives “ Alert ” dialogs for confirmation or Stage dialogs for data entry (and click the save button). After the event, I usually showed another Alert with "Success" (if it was successful).
Now, to eliminate a bunch of unnecessary “useless” windows / screens / pop-ups, I wanted to minimize Alert or Stage in the lower left corner of the screen, where the message “Success” will appear for about 3 seconds in the status bar. I implemented this successfully, but I noticed a huge performance difference between animation on Alert and animation on Stage .
Alert seems very lively, and the Stage is actually very volatile (even on a good PC). I read about caching and looked for related questions, but without much effect or solutions. I tried to make a JavaFX (Maven) example (based on some other examples I found) that you can find below.
You will see when you click the “Show alert” button and click “Yes” in the Alert window, Alert will go smoothly to the lower left corner of the screen. When you click the Show node button and click the Close button on the newly opened stage, the animation will be much more choppy than Alert .
Is there anything that can be done to smooth out the animation of the scene? I also tried to make the top AnchorPane invisible to see if there was any performance improvement, but it was exactly the same.
Scene.fxml:
<?xml version="1.0" encoding="UTF-8"?> <?import java.lang.*?> <?import java.util.*?> <?import javafx.scene.*?> <?import javafx.scene.control.*?> <?import javafx.scene.layout.*?> <AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.40" fx:controller="proj.mavenproject1.FXMLController"> <children> <Button fx:id="button" layoutX="52.0" layoutY="90.0" onAction="#handleButtonAction" text="Show Alert" /> <Label fx:id="label" layoutX="126" layoutY="120" minHeight="16" minWidth="69" /> <Button fx:id="button1" layoutX="217.0" layoutY="90.0" onAction="#handleButtonAction2" text="Show Node" /> </children> </AnchorPane>
testNode.fxml:
<?xml version="1.0" encoding="UTF-8"?> <?import java.lang.*?> <?import java.util.*?> <?import javafx.scene.*?> <?import javafx.scene.control.*?> <?import javafx.scene.layout.*?> <AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.40" fx:controller="proj.mavenproject1.TestNodeController"> <children> <Button layoutX="262.0" layoutY="188.0" mnemonicParsing="false" onAction="#handleButtonAction" text="Close node" /> </children> </AnchorPane>
FXMLController.java:
package proj.mavenproject1; import java.io.IOException; import java.net.URL; import java.util.ResourceBundle; import java.util.logging.Level; import java.util.logging.Logger; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.Initializable; import javafx.scene.control.Label; public class FXMLController implements Initializable { @FXML private Label label; @FXML private void handleButtonAction(ActionEvent event) { Utilities.showYesNo("test", "this to test the closing animation of an alert", true); System.out.println("You clicked me!"); label.setText("Hello World!"); } @FXML private void handleButtonAction2(ActionEvent event) { try { URL url = getClass().getResource("/fxml/testNode.fxml"); Utilities.showDialog(url); } catch (IOException ex) { Logger.getLogger(FXMLController.class.getName()).log(Level.SEVERE, null, ex); } } @Override public void initialize(URL url, ResourceBundle rb) {
TestNodeController.java:
package proj.mavenproject1; import java.net.URL; import java.util.ResourceBundle; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.Initializable; public class TestNodeController implements Initializable { @FXML private void handleButtonAction(ActionEvent event) { Utilities.closeStage(event, true); } @Override public void initialize(URL url, ResourceBundle rb) {
Utilities.java:
package proj.mavenproject1; import java.io.IOException; import java.net.URL; import java.util.Optional; import java.util.ResourceBundle; import javafx.animation.KeyFrame; import javafx.animation.KeyValue; import javafx.animation.Timeline; import javafx.beans.property.DoubleProperty; import javafx.beans.property.SimpleDoubleProperty; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.beans.value.WritableValue; import javafx.event.ActionEvent; import javafx.event.Event; import javafx.event.EventHandler; import javafx.fxml.FXMLLoader; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.CacheHint; import javafx.scene.Node; import javafx.scene.Scene; import javafx.scene.control.Alert; import javafx.scene.control.ButtonType; import javafx.scene.control.DialogEvent; import javafx.scene.layout.AnchorPane; import javafx.scene.layout.VBoxBuilder; import javafx.stage.Modality; import javafx.stage.Screen; import javafx.stage.Stage; import javafx.stage.StageStyle; import javafx.stage.WindowEvent; import javafx.util.Duration; public class Utilities { public static boolean showYesNo(String title, String content, boolean animation) { Alert alert = new Alert(Alert.AlertType.CONFIRMATION); alert.setTitle(title); alert.setHeaderText(null); alert.setContentText(content); alert.getButtonTypes().setAll(ButtonType.YES, ButtonType.NO); alert.setOnCloseRequest((DialogEvent we) -> { if (animation) { minimizeAlert(alert, animation); we.consume(); } }); Optional<ButtonType> result = alert.showAndWait(); return result.get() == ButtonType.YES; } public static void showDialog(URL url) throws IOException { final Stage myDialog = new Stage(); myDialog.initStyle(StageStyle.UTILITY); myDialog.initModality(Modality.APPLICATION_MODAL); Node n = (Node) FXMLLoader.load(url); Scene myDialogScene = new Scene(VBoxBuilder.create().children(n).alignment(Pos.CENTER).padding(new Insets(0)).build()); myDialog.setScene(myDialogScene); myDialog.showAndWait(); } private static void minimizeNode(Scene scene, boolean animation) { final int MILLIS = 750;