How can I change a scene without directly interacting with it (like using a button). What I would like for my program to do is change scene, and then using the Initializable
class, the program would wait 1.5 seconds then change scene.
I am currently using a PauseTransition
to do this, however I get a ClassCastException
. Also, I do not understand what Intellij is trying to tell me about the Node. It says 'Find why 'event.getSource()' could be javafx.animation.PauseTransition (not-null)'.
Thanks in advance.
Stack Trace:
Exception in thread "JavaFX Application Thread" java.lang.ClassCastException: class javafx.animation.KeyFrame cannot be cast to class javafx.scene.Node (javafx.animation.KeyFrame and javafx.scene.Node are in module javafx.graphics of loader 'app')
at Dice.Game/sample.RoundCounterController.lambda$initialize$0(RoundCounterController.java:54)
at javafx.graphics/com.sun.scenario.animation.shared.TimelineClipCore.visitKeyFrame(TimelineClipCore.java:239)
at javafx.graphics/com.sun.scenario.animation.shared.TimelineClipCore.playTo(TimelineClipCore.java:180)
at javafx.graphics/javafx.animation.Timeline.doPlayTo(Timeline.java:177)
at javafx.graphics/javafx.animation.AnimationAccessorImpl.playTo(AnimationAccessorImpl.java:39)
at javafx.graphics/com.sun.scenario.animation.shared.SingleLoopClipEnvelope.timePulse(SingleLoopClipEnvelope.java:99)
at javafx.graphics/javafx.animation.Animation.doTimePulse(Animation.java:1101)
at javafx.graphics/javafx.animation.Animation$1.lambda$timePulse$0(Animation.java:186)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/javafx.animation.Animation$1.timePulse(Animation.java:185)
at javafx.graphics/com.sun.scenario.animation.AbstractMasterTimer.timePulseImpl(AbstractMasterTimer.java:344)
at javafx.graphics/com.sun.scenario.animation.AbstractMasterTimer$MainLoop.run(AbstractMasterTimer.java:267)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:515)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:499)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.pulseFromQueue(QuantumToolkit.java:492)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$11(QuantumToolkit.java:320)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
at java.base/java.lang.Thread.run(Thread.java:829)
RoundCountController.java
import javafx.animation.KeyFrame;
import javafx.animation.PauseTransition;
import javafx.animation.Timeline;
import javafx.animation.Transition;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.util.Duration;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
public class RoundCounterController implements Initializable {
@FXML private Label roundLabel;
private static int roundCounter = 0;
@FXML private AnchorPane pane;
private Stage stage;
private Scene scene;
private Parent root;
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
PauseTransition pauseTransition = new PauseTransition(Duration.seconds(1.5));
pauseTransition.setOnFinished(event -> {
try {
Parent root = FXMLLoader.load(getClass().getResource("p1GameScene.fxml"));
} catch (IOException e) {
e.printStackTrace();
}
stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
scene = new Scene(root);
stage.setScene(scene);
stage.show();
});
pauseTransition.play();
}
}