Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating two text objects in JavaFX, one field after another, only seeing final result of both changes

Tags:

javafx

I have three text fields displayed, and I want to change the second one, see the result on the display (so wait a couple of seconds), then change the third one, and see the result on the display. Instead, I only see the result of both changes on the display (with no pause inbetween).

import javafx.application.Application;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.animation.PauseTransition;
import javafx.util.Duration;
import javafx.scene.Scene; 
import javafx.scene.layout.VBox;

public class TestApp extends Application
{
    private Text tone = new Text("one");
    private Text ttwo = new Text("two");
    private Text tthree = new Text("three");

    private void process()
    {
        PauseTransition pauseTransition = new PauseTransition(Duration.seconds(2));
        pauseTransition.setOnFinished(event -> ttwo.setText("four"));
        pauseTransition.play();

        pauseTransition = new PauseTransition(Duration.seconds(2));
        pauseTransition.setOnFinished(event -> tthree.setText("five"));
        pauseTransition.play();
   } // end of method "process"

    @Override
    public void start(Stage stage) 
    {
        VBox vboxRoot = new VBox();
        vboxRoot.getChildren().add(tone);
        vboxRoot.getChildren().add(ttwo);
        vboxRoot.getChildren().add(tthree);
        Scene myScene = new Scene(vboxRoot,350,350);
        stage.setScene(myScene);
        stage.setTitle("Test");
        stage.show();
        process();
    } // end of method "start"

} // end of class "TestApp"

So initially

one
two
three

is displayed; followed by

one
four
five

What I want to see is

one
four
three

a pause and then

one
four
five
like image 318
jamxl5 Avatar asked Jan 27 '23 01:01

jamxl5


1 Answers

I'm not sure if its a typo if your What I want to see is but if its not the reason you are getting

one
two 
three 

to initially display is because thats what you have them set as and in this piece of code below you setup 2 PauseTransitions that both have a 2 second wait before changing the text

private void process()
{
    PauseTransition pauseTransition = new PauseTransition(Duration.seconds(2));
    pauseTransition.setOnFinished(event -> ttwo.setText("four"));
    pauseTransition.play();

    pauseTransition = new PauseTransition(Duration.seconds(2));
    pauseTransition.setOnFinished(event -> tthree.setText("five"));
    pauseTransition.play();
}

To fix this you can do a few things such as

  1. Appropriately set what you want from the start
  2. Run ttwo.setText("four"); at the start of your process() method

After doing that you get the starting result of

one
four
three

and after the pause transition finishes 2 seconds later you will see

one
four
five
like image 165
Matt Avatar answered Feb 04 '23 16:02

Matt