-1

I am building an application using JavaFx that displays the current date and time in the top corner of the scene.

But it Display an IllegalStateException Error. Here's my code.

@Override
public void initialize(URL arg0, ResourceBundle arg1) {

    new Thread(new Runnable() {

        @Override
        public void run() {

             while(true){
                Date d=new Date();
                String date1=d.toString();
                String arr[]=date1.split(" ");
                String dd= arr[5]+"/"+arr[1]+"/"+arr[2];
                date.setText(dd);
                time.setText(arr[3]);
                }
            }
    }).start();
}

}

I Don't Know How to Use Platform.runLater(...) for This Code.

Yohan Neranga
  • 141
  • 1
  • 2
  • 10
  • 1
    Use `Platform.runLater()` (no-one else here knows why you don't know how to use it), or (probably better) use an animation to update your labels (or whatever they are). Also, use the proper API for [date and time](http://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) and for [formatting them](http://docs.oracle.com/javase/8/docs/api/java/time/format/package-frame.html), don't parse the result of `toString()` methods. Finally, don't use busy while loops. – James_D Sep 13 '17 at 14:18

1 Answers1

0

To answer your question, you would use Platform.runLater around this code:

date.setText(dd);
time.setText(arr[3]);

However as correctly mentioned in the comments, you should probably use animations. Or do some sleep in your loop, because right now your code will only make your computer heat your room.

findusl
  • 2,454
  • 8
  • 32
  • 51