Question: I have a javafx application that starts a new thread on button click.

**Here's the method that starts the thread:**

```
private void startTest() {
        String[] args = {};
        String threadId = TestRunner.class.getSimpleName();
        mainApp.setRunner(new Thread(() -> {
            try {
                TestRunner.main(args);
            } catch (Throwable e) {
                LOGGER.fatal(e.getMessage(), e);
            }
        }, threadId));
        mainApp.getRunner().start();
    }

```

I can get and set `Thread` object from the mainApp using the `setRunner()` and `getRunner()` accessors.

**This is what's currently happening:**
- I launch the application
- I click the button `[START]`- Upon clicking, a new thread will start and I will change the text of the `[START]` button to `[PAUSE]`- I know for a fact the the thread will finish at some point of time (when the tasks completes)

**What I want to achieve:**
- I would like to change the text of the now `[PAUSE]` button to `[START]` after the thread is completed / when the thread dies.
- A few other more things that are dependent on the status of the thread

How can I do these?

I'm currently playing around observable boolean value but I could use some help to achieve this.

Thanks in advance!

Answer: 
Use a `Service` to manage the background thread in the main app (or wherever you are currently managing the `runner` property). Something like:

```
public class MainApp {

    private final Service<Void> testService = new Service<Void>() {
        @Override
        protected Task<Void> createTask() {
            return new Task<Void>() {
                @Override
                protected Void call() throws Exception {
                    // code to execute on background thread here:
                    while (! isCancelled() ) {
                        // ...
                    }
                }
            }
        }
    };

    public Service<Void> getTestService() {
        return testService();
    }

    // ...
}

```

Then in your UI all you need to do is

```
Button startButton = new Button();
startButton.textProperty().bind(Bindings.
    when(mainApp.getTestService().runningProperty()).
    then("Pause").
    otherwise("Start"));
startButton.setOnAction(e -> {
    if (mainApp.getTestService().isRunning()) {
        mainApp.getTestService().cancel();
    } else {
        mainApp.getTestService().restart();
    }
});

```

The [`Task` documentation](http://docs.oracle.com/javase/8/javafx/api/javafx/concurrent/Task.html) has plenty of information and examples of how to implement the `Task`s that are returned from the `Service`'s `createTask()` method.
**Change values of nodes inner threads :**

You have two choices to do what you want ,you can use Task class and bind your button with changing of value of your task ,you have updateMessage("") and updateValue() or updateTitle ,or you can use Platform class inner your thread.

```
        Button StartButton=new Button("Start");
        Thread TestThread=new Thread(new Runnable(){
           @Override
           public void run()
             {
              Platform.runLater(new Runnable(){
           @Override
           public void run()
             {

             StartButton.setText("Pause");    

             }
             while(true)
            {
              }

            }
         );
        StartButton.addEventHandler(event->{
             if(StartButton.getText().equals("Start"))
             {
              StartThread.start();
             }else{
             StartThread.interrupt();  
              }

```