title: How to add progressbar to Javafx
body: <p>I am building a desktop app using javafx, I am downloading a file around 500 MB using ftp. I need to show the progress bar with % while downloading is in progress. I also need to give a option to cancel a ongoing downloading process.
  I want the progress bar when the download button is clicked and it show the download progress and if I cancel downloading should stop.</p>
  
  <p>This is my code to download file.</p>
  
  <pre><code>                    downloadButton.setOnMouseClicked(new EventHandler&lt;MouseEvent&gt;() {
  
          @Override
          public void handle(MouseEvent event) {
              // TODO Auto-generated method stub
  
              FTPClient ftpClient = new FTPConnection().makeConnection();
  
              try {
                  ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
                  success = ftpClient.changeWorkingDirectory(PATH + preset + "/" + file_to_download + offset);
                  System.out.println("Download Path:-" + PATH + preset + "/" + file_to_download + offset);
                  if (!success) {
                      System.out.println("Could not changed the directory to RIBS");
                      return;
                  } else {
                      System.out.println("Directory changed to RIBS");
                  }
                  FTPFile[] files = ftpClient.listFiles();
                  for (FTPFile file : files) {
                      if (file.getName().contains(".zip")) {
                          dfile = file.getName();
                      }
  
                  }
  
                  DirectoryChooser dirChooser = new DirectoryChooser();
                  primaryStage = (Stage) tableView.getScene().getWindow();
                  primaryStage.setTitle("Background Processes");
                  File chosenDir = dirChooser.showDialog(primaryStage);
                  System.out.println(chosenDir.getAbsolutePath());
  
                  Task task = new Task&lt;Void&gt;() {
                      @Override
                      public Void call() throws IOException {
                          try {
                              output = new FileOutputStream(chosenDir.getAbsolutePath() + "/" + dfile);
                          } catch (FileNotFoundException e) {
                              // TODO Auto-generated catch block
                              e.printStackTrace();
                          }
                          ftpClient.sendNoOp();
                          ftpClient.setConnectTimeout(1000);
  
                          int filesize = 0;
                          if (ftpClient.retrieveFile(dfile, output) == true) {
                              downloadButton.setDisable(true);
                              dfile.length();
                              System.out.println("FILE:LENGTH" + dfile.length());
                          }
  
                          // updateProgress(outByte, inputByte);
  
                          return null;
                      }
                  };
                                      final Stage dialog = new Stage();
                  dialog.initModality(Modality.WINDOW_MODAL);
                  dialog.initOwner(primaryStage);
                  final ProgressBar progressBar = new ProgressBar(0);
  
                  final Button cancelButton = new Button("Cancel");
                  cancelButton.setOnAction(new EventHandler&lt;ActionEvent&gt;() {
                      public void handle(ActionEvent event) {
                          copyWorker.cancel(true);
                          progressBar.progressProperty().unbind();
                          progressBar.setProgress(0);
                          System.out.println("cancelled.");
                          dialog.close();
                      }
                  });
                  final Button closeButton = new Button("Close");
                  closeButton.setOnAction(new EventHandler&lt;ActionEvent&gt;() {
                      public void handle(ActionEvent event) {
                          progressBar.progressProperty().unbind();
                          progressBar.setProgress(0);
                          dialog.close();
                      }
                  });
  
                  cancelButton.setDisable(false);
                  closeButton.setDisable(true);
  
                  VBox dialogVbox = new VBox();
                  VBox.setMargin(progressBar, new Insets(10, 10, 10, 10));
                  VBox.setMargin(cancelButton, new Insets(10, 10, 10, 10));
                  dialogVbox.setAlignment(Pos.CENTER);
                  HBox hBox = new HBox();
                  hBox.setAlignment(Pos.CENTER);
                  hBox.getChildren().addAll(cancelButton, closeButton);
  
                  dialogVbox.getChildren().addAll(progressBar, hBox);
  
                  Scene dialogScene = new Scene(dialogVbox, 160, 100);
                  dialog.initStyle(StageStyle.UNDECORATED);
                  dialog.setResizable(false);
                  dialog.setScene(dialogScene);
                  dialog.setOnShown(e -&gt; {
  
                      progressBar.setProgress(0);
                      copyWorker = createWorker();
  
                      progressBar.progressProperty().unbind();
                      progressBar.progressProperty().bind(copyWorker.progressProperty());
  
                      copyWorker.messageProperty().addListener(new ChangeListener&lt;String&gt;() {
                          public void changed(ObservableValue&lt;? extends String&gt; observable, String oldValue, String newValue) {
                              System.out.println(newValue);
  
                          }
                      });
  
                      // TODO: handle succeeded &amp; failed depending on your needs
                      EventHandler doneHandler = new EventHandler() {
  
                          @Override
                          public void handle(Event event) {
  
                              cancelButton.setDisable(true);
                              closeButton.setDisable(false);
  
                          }
  
                      };
                      copyWorker.setOnSucceeded(doneHandler);
                      copyWorker.setOnFailed(doneHandler);
  
                      new Thread(copyWorker).start();
  
                  });
                  dialog.show();
                  // end
                  new Thread(task).start();
  
                  if (output != null) {
                      output.close();
                  }
  
              } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
          }
  
      });
  </code></pre>
  
  <p>In reference to my question <a href="https://stackoverflow.com/questions/35388850/how-to-show-a-progress-bar-while-downloading-in-javafx">How to show a progress bar while downloading in javafx</a>
  I have moved my download code into a different background thread but I am not able to figure out how to display the progress bar.</p>