cross posted here http://www.coderanch.com/t/607695/java/java/Calender-getInstance-system-time-autoupdate#2775075
I have been trying to get the time, and update it(like making my own digital clock).
Okay, so I tried using the ScheduledThreadPoolExecutor, and it worked great! The only issue is that I'm having trouble updating the calender.
NOTE: Lambda expressions are used as "->" so runnable and run have been removed.
public class Timer extends Application
{
// Timer t = new Timer();
Calendar rightNow = Calendar.getInstance();
@Override
public void start(Stage primaryStage)
{
Button btn = new Button();
ScheduledThreadPoolExecutor stpe = new ScheduledThreadPoolExecutor(1);
System.out.println(stpe.getCorePoolSize());
stpe.scheduleAtFixedRate(() -> {
btn.setText("" + rightNow.getTime());
System.out.println(rightNow.getTime());
//rightNow = Calendar.getInstance();
},0, 1,TimeUnit.SECONDS);
btn.setOnAction((ActionEvent event) -> {
// rightNow = Calendar.getInstance();
});
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
Basically if I run it normally it will just return the instance at that one second, and wont update with rightNow.getTime() and will just pop out the same time constantly.
If I try to set
rightNow = Calendar.getInstance();
inside the setFixedRate it wont run the scheduler, and if I click the button after I've seen it update the instance a few times, it will then stop right away.
Why exactly is my ScheduledThreadPoolExecutor breaking when I'm getting my instance? Is there another way I should go about this maybe?