spinner lambda javafx

Solutions on MaxInterview for spinner lambda javafx by the best coders in the world

showing results for - "spinner lambda javafx"
Florencia
21 Jan 2016
1package com.zetcode;
2
3import java.util.Timer;
4import java.util.TimerTask;
5import javafx.application.Application;
6import javafx.application.Platform;
7import javafx.geometry.Insets;
8import javafx.scene.Scene;
9import javafx.scene.control.Alert;
10import javafx.scene.control.Button;
11import javafx.scene.control.Spinner;
12import javafx.scene.layout.HBox;
13import javafx.stage.Stage;
14
15public class TimerEx extends Application {
16
17    int delay = 0;
18
19    @Override
20    public void start(Stage stage) {
21
22        initUI(stage);
23    }
24
25    private void initUI(Stage stage) {
26
27        var root = new HBox(10);
28        root.setPadding(new Insets(10));
29
30        var timer = new Timer();
31
32        var spinner = new Spinner<>(1, 60, 5);
33        spinner.setPrefWidth(80);
34
35        var btn = new Button("Show message");
36        btn.setOnAction(event -> {
37
38            delay = (int) spinner.getValue();
39            timer.schedule(new MyTimerTask(), delay*1000);
40        });
41
42        root.getChildren().addAll(btn, spinner);
43
44        stage.setOnCloseRequest(event -> timer.cancel());
45
46        var scene = new Scene(root);
47
48        stage.setTitle("Timer");
49        stage.setScene(scene);
50        stage.show();
51    }
52
53    private class MyTimerTask extends TimerTask {
54
55        @Override
56        public void run() {
57
58            Platform.runLater(() -> {
59
60                var alert = new Alert(Alert.AlertType.INFORMATION);
61                alert.setTitle("Information dialog");
62                alert.setHeaderText("Time elapsed information");
63
64                String contxt;
65
66                if (delay == 1) {
67                    contxt = "1 second has elapsed";
68                } else {
69                    contxt = String.format("%d seconds have elapsed",
70                            delay);
71                }
72
73                alert.setContentText(contxt);
74                alert.showAndWait();
75            });
76        }
77    }
78
79    public static void main(String[] args) {
80        launch(args);
81    }
82}
83