Access a method that is outside the thread

Asked

Viewed 74 times

2

I have a video running in my application and a Thread running a Socket(server), all within the same class, but I need this Thread to access a method that is outside of it.

To be more exact I want to run getCurrentTime() to take the current time when the video is running and send by Socket to another application.

1 answer

1


To have access to the UI you must ask the UI API to run a certain code in the UI thread, in the case of Javafx follow the excerpt below.

Platform.runLater(new Runnable() {
    @Override public void run() {
        //Acessar componente de vídeo aqui    
    }
});

or in Java 8

 Platform.runLater(() -> {
       //Acessar componente de vídeo aqui    
 }));

The detail is that this code will be executed when the Javafx API finds it best.

  • Even if you run in an environment other than Java 8, I really recommend using lambda notation. You can do lambda backward compatibility with previous versions using the Retrolambda

Browser other questions tagged

You are not signed in. Login or sign up in order to post.