// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class JavaFXLabelUpdate extends Application {
    private Label lbl;
    private Button btn;
    @Override
    public void start(Stage stg) throws Exception {
        stg.setTitle("Label update");
        GridPane p = new GridPane();
        lbl = new Label("Click start to start");
        p.add(lbl, 0, 0);
        btn = new Button("Start");
        btn.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent ev) {
                Thread t = new Thread(new Runnable() {
                    public void run() {
                        for(int i = 1; i <= 60; i++) {
                            try {
                                Thread.sleep(1000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            final int i2 = i;
                            Platform.runLater(new Runnable() {
                                public void run() {
                                    lbl.setText(Integer.toString(i2));
                                }
                            });
                        }
                    }
                });
                t.start();
            }
        });
        p.add(btn, 0, 1);
        Scene scene = new Scene(p);
        stg.setScene(scene);
        stg.sizeToScene();
        stg.show();
    }
    public static void main(String[] args) {
        Application.launch(args);
    }
}
