title: Start and stop JavaFX background thread with 2 buttons
body: <p>I am trying to make a simple UI to launch a selenium test that has the ability to start a background thread which launches a browser when the Start Button is pressed and stops the thread and closes it when the Stop button is pressed. 
  Unfortunately when I click stop after starting it, it does not work. If I let it finish I cannot restart the thread. How would I go about updating this so that I can make it submit a new thread that can be stopped by the stop button.</p>
  
  <pre><code>        package application;
  
  import org.openqa.selenium.WebDriver;
  
  import javafx.application.Application;
  import javafx.concurrent.Task;
  import javafx.event.ActionEvent;
  import javafx.event.EventHandler;
  import javafx.geometry.Insets;
  import javafx.stage.Stage;
  import javafx.scene.Scene;
  import javafx.scene.control.Button;
  import javafx.scene.control.Label;
  import javafx.scene.control.TextField;
  import javafx.scene.layout.BorderPane;
  import javafx.scene.layout.GridPane;
  
  
  public class Main extends Application {
      Stage window;
      GridPane grid;
      public void start(Stage primaryStage) {
  
          /*
           * Set up the stage
           */
          window = primaryStage;
          window.setTitle("URL LOADER - V1");
          grid = new GridPane();
          grid.setPadding(new Insets(10,10,10,10));
          grid.setVgap(8);
          grid.setHgap(10);
          window.setResizable(false);
  
          /*
           * URL input
           */
          Label URLLabel = new Label("URL");
          GridPane.setConstraints(URLLabel,0,0);
          TextField URLTextField = new TextField();
          URLTextField.setPromptText("https://www.google.com");
          GridPane.setConstraints(URLTextField,1,0);
  
          /*
           * Create Buttons
           */
          Button buttonStart = new Button("Create");
          GridPane.setConstraints(buttonStart,1,6);
          Button buttonStop = new Button("Stop");
          GridPane.setConstraints(buttonStop,1,8);
  
          grid.getChildren().addAll(URLLabel,URLTextField, buttonStart, buttonStop);
  
          /*
           * Create the scene
           */
          Scene scene = new Scene(grid, 300, 300);
          window.setScene(scene);
          window.show();
  
          Task&lt;Void&gt; task = new Task&lt;Void&gt;(){
              @Override
              protected Void call() {
                  new VisitPage().Start(this,URLTextField.getText());;
                  return null;
              }
          };
  
          buttonStart.setOnAction(new EventHandler&lt;ActionEvent&gt;() {
          /*
           * Start Button Clicked
           */
          public void handle(ActionEvent event) {
  
              new Thread(task).start();
  
          }
  
      });
  
          buttonStop.setOnAction(new EventHandler&lt;ActionEvent&gt;() {
              /*
               * Start Button Pressed
               */
              public void handle(ActionEvent event) {
                  System.out.println("Stop Pressed");
  
              }
          });
  
      }
  
      public class VisitPage {
          private String URL;
          Browser BrowserFactory;
          ThreadLocal&lt;WebDriver&gt; drivers;
          WebDriver Browser;
          public void Start(Task&lt;Void&gt; task, String URL) {
              while (true) {
                  if (task.isCancelled())
                  {
                          System.out.println("Canceling...");
                          System.out.println("Stop Pressed");
                          Browser.close();
                          Browser.quit();
                          BrowserFactory.CloseDriver(drivers);
                          task.cancel();
                  }
                  else
                  {
              /*
               * Create Browser Factor to make ThreadLocal Browsers
               */
              BrowserFactory = new Browser(1, 1);
              drivers = BrowserFactory.SpawnBrowser();
              /*
               * Grab a Browser
               */
              Browser = BrowserFactory.SpawnDriver(drivers);
              /*
               * Visit and scrape
               */
              Browser.get(URL);
              /*
               * Wait 5 Seconds before closing
               */
              try {
                  Thread.sleep(5000);
              } catch (InterruptedException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
  
              Browser.close();
              Browser.quit();
              BrowserFactory.CloseDriver(drivers);
                   }
  
               }
          }
  
      }
  
  
      public static void main(String[] args) {
          launch(args);
      }
  }
  </code></pre>