title: Using Javafx TimeLine In a Thread
body: <p>I'm trying to create an internet checker class, that will check the connection to a certain url and update the status property accordingly. To avoid ui freeze i want to use a thread, and a timer to recheck after a certain interval. Problem is the CHECK method call in the timeline keyframe is called from the FX thread still. How can i use a timeline inside a thread?</p>
  
  <p><strong>CODE:</strong></p>
  
  <pre><code>public class InternetChecker {
      private String baseUrl;
      /***Properties***/
      private ObjectProperty&lt;Status&gt; status = new SimpleObjectProperty&lt;&gt;(Status.ACTIVE);
  
      /****************************************************************
       **********                  CONSTRUCTORS            ************
       ****************************************************************/
      public InternetChecker(String baseUrl) {
          this(baseUrl, 1000);
      }
  
      public InternetChecker(String baseUrl, int millisCheckInterval) {
          this.baseUrl = baseUrl;
          new Thread(() -&gt; {
              Timeline timelineCheck = new Timeline();
              timelineCheck.getKeyFrames().add(
                      new KeyFrame(Duration.millis(millisCheckInterval), e -&gt; {
                          check();
                      }));
              timelineCheck.setCycleCount(Animation.INDEFINITE);
              timelineCheck.play();
          }).start();
      }
  
      /*******************************
       * Will check if there is an internet connection present
       * and update the status accordingly
       *******************************/
      public void check() {
          // Check if base internet connection
          // is working, if it is we continue
          // to see if domain connection is working
          try {
              if ("127.0.0.1".equals(InetAddress.getLocalHost().getHostAddress())) {
                  setStatus(Status.INTERNET_DISCONNECTED);
                  return;
              }
          } catch (UnknownHostException e) {
              throw new RuntimeException(e);
          }
          // Check if base domain connection is working
          try {
              final URL url = new URL(baseUrl);
              final URLConnection conn = url.openConnection();
              conn.connect();
              conn.getInputStream().close();
              setStatus(Status.ACTIVE);
          } catch (MalformedURLException e) {
              throw new RuntimeException(e);
          } catch (IOException e) {
              setStatus(Status.BASE_URL_UNREACHABLE);
          }
      }
  
      /****************************************************************
       **********                  ACCESSORS               ************
       ****************************************************************/
  
      public Status getStatus() {
          return status.get();
      }
  
      public ObjectProperty&lt;Status&gt; statusProperty() {
          return status;
      }
  
      private void setStatus(Status status) {
          this.status.set(status);
      }
  
      /*******************************
       *  ACTIVE (Base url reachable)
       *  BASE_URL_UNREACHABLE (Internet available, but base url is unreachable)
       *  INTERNET_DISCONNECTED (Internet is not available)
       ********************************/
      public enum Status {
          ACTIVE,
          BASE_URL_UNREACHABLE,
          INTERNET_DISCONNECTED;
      }
  }
  </code></pre>