Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. package view;
  2.  
  3. import java.time.LocalTime;
  4. import java.time.format.DateTimeFormatter;
  5. import java.util.Timer;
  6. import java.util.TimerTask;
  7.  
  8. import javafx.animation.KeyFrame;
  9. import javafx.animation.KeyValue;
  10. import javafx.animation.Timeline;
  11. import javafx.application.Platform;
  12. import javafx.event.ActionEvent;
  13. import javafx.event.Event;
  14. import javafx.event.EventHandler;
  15. import javafx.geometry.Pos;
  16. import javafx.scene.control.Label;
  17. import javafx.scene.layout.FlowPane;
  18. import javafx.scene.shape.Rectangle;
  19. import javafx.util.Duration;
  20.  
  21. public class WelcomeFP extends FlowPane{
  22.  
  23. private Label lblTime;
  24. private Timeline tlMinute;
  25. private Rectangle rect;
  26.  
  27. public WelcomeFP() {
  28. super();
  29.  
  30. this.getChildren().add(getLblTime());
  31. this.setAlignment(Pos.TOP_CENTER);
  32.  
  33. }
  34.  
  35. public Label getLblTime() {
  36. if(lblTime == null) {
  37. lblTime = new Label("Time: ");
  38. /*
  39. Thread timeThread = new Thread(new Runnable() {
  40.  
  41. @Override
  42. public void run() {
  43.  
  44. while(true) {
  45.  
  46. Platform.runLater(new Runnable() {
  47.  
  48. @Override
  49. public void run() {
  50. lblTime.setText(LocalTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss")));
  51. }
  52. });
  53.  
  54. try {
  55. if(Thread.interrupted()) {
  56. return;
  57. }
  58. Thread.sleep(1000);
  59. } catch (InterruptedException e) {
  60. return;
  61. }
  62. }
  63. }
  64. });
  65. timeThread.setDaemon(true);
  66. timeThread.start();
  67. */
  68.  
  69. /*
  70. TimerTask task = new TimerTask() {
  71.  
  72. @Override
  73. public void run() {
  74. //while(true) {
  75.  
  76. Platform.runLater(new Runnable() {
  77.  
  78. @Override
  79. public void run() {
  80. lblTime.setText(LocalTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss")));
  81. }
  82. });
  83. /*
  84. try{
  85. if(Thread.interrupted()) {
  86. return;
  87. }
  88. Thread.sleep(1000);
  89. } catch (InterruptedException e) {
  90. return;
  91. }
  92.  
  93. //}
  94.  
  95. }
  96. };
  97. Timer timer = new Timer(true); // pour le rendre deamon thread
  98. timer.scheduleAtFixedRate(task, 0, 1000);
  99. */
  100. KeyFrame keyFrame = new KeyFrame(Duration.seconds(1), new EventHandler<ActionEvent>() {
  101.  
  102. @Override
  103. public void handle(ActionEvent arg0) {
  104. lblTime.setText(LocalTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss")));
  105.  
  106. }
  107. });
  108. Timeline timeLine = new Timeline(keyFrame);
  109. timeLine.setCycleCount(Timeline.INDEFINITE);
  110. timeLine.playFromStart();
  111. }
  112. return lblTime;
  113. }
  114.  
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement