0

Following is the code package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));

        Scene scene = new Scene(root);
        primaryStage.setTitle("Change me");

        primaryStage.setScene(scene);
        primaryStage.show();

        for(long i=0;i<3;i++){
            primaryStage.setTitle("Change title to "+i);
            try{
                Thread.sleep(1000);
            }
            catch (Exception e){
                e.printStackTrace();
            }
        }
    }

}

The intention is to start the window and then change the title, but what happens is that first the loop executes and then window is shown.

BEWARB
  • 131
  • 1
  • 10

1 Answers1

0

Apart from this, Using normal thread for computations and JavaFX thread(i.e., Platform.runlater()) for UI updation is another solution.

Using this tutorial, I was able to complete the task. The reason is directly quoted from the tutorial -

It is repeated to emphasize that all UI event handlers in JavaFX run on a single thread, which is the JavaFX Application Thread.

New code is

package sample;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));

        Scene scene = new Scene(root);
        primaryStage.setTitle("Change me");

        primaryStage.setScene(scene);
        primaryStage.show();
        System.out.println("shown");
        Thread thread = new Thread(){
            public void run(){
                for (long i = 0; i < 6; i++) {
                    long finalI = i;
                    Platform.runLater(()-> {
                        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
                        Date date = new Date();
                        System.out.println(dateFormat.format(date));
                        primaryStage.setTitle("Change title to " + finalI);
                    });
                    try {
                        Thread.sleep(1000);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        thread.start();
    }

}
BEWARB
  • 131
  • 1
  • 10
  • 1
    typically, using plain threads is way too low-level, instead use fx concurrency support - f.i. Timeline. And while you are at it: switch to the new Date/Time support :) – kleopatra Mar 08 '20 at 12:56
  • kleopatra, I've edited the answer – BEWARB Mar 08 '20 at 17:47