How to run progressbar on modal window form FXML?
<p>First I would like to apology if this question have obvious answer but I didn't found a proper amswer in Internet. I have followed some tutorials but I bumped into an issue and could not understand where I am wrong. I am trying to run Multithreaded desktop application where there is a task (TaskGetRetailersURL which implements Runnable )  where I want to put progressbar on modal window. I have a main thread where my application is running. On this MAIN thread I start new Thread for this task - TaskGetRetailersURL and another second Thread for the Modal window - TaskModalWindow also implements runnable. The reason I start them on two threads is that if the Modal window is not on another Thead the modal window is blocked is showing "Not responding!". TaskGetRetailersURL is taking some time to collect some data x items which are needed for the size of progressbar ( I dont know how many items will be and I dont know the time it will be needed.) I am Using a flag and while loop to wait for this to initialize the parameters in the controller. I want after Click on Search button the Modal window to show ( I dont want to wait until the data x is collected. It is OK if the window shows and the value of progressbar to be 0. ). If I execute the Methods progressWindowController.setProgressBarListener(); and progressWindowController.setProgressIndicator(); progressWindowController.startTask(); Now with this code the modal window shows after the  TaskGetRetailersURL completes and the data is collected.</p>

<ol>
<li>What I am doing wrong?</li>
<li>How to make to show the modal window with the progress bar in the beggining after <code>startSearchUsers(ActionEvent event)</code> method is ivoked and not shows in the end of the Task? There is a time slot of 30 seconds before it shows. </li>
</ol>

<p>I send the files.:
UsersTabController.java - Here is seacrh method where the Main Thread runs.</p>

<pre><code>package com.mjp.java.controllers;


@Component
public class UsersTabController implements FxmlController {

    @FXML
    private TextField fromRating;
    @FXML
    private TextField toRating;
    @FXML
    private TextField searchWord;

    @FXML
    private Tab usersTab;

    @FXML
    private ComboBox&lt;String&gt; valuesComboBox;



    private final ExceptionWriter exceptionsWriter;

    @Autowired
    private RetailerDao retailerDao;



    public static ExecutorService executorUsers = Executors.newFixedThreadPool(CrawlerConstants.MYTHREADS);

    @Autowired
    public UsersTabController(ExceptionWriter exceptionsWriter) {
        this.exceptionsWriter = exceptionsWriter;
    }



    @Override
    public void initialize() {
        valuesComboBox.getItems().removeAll(valuesComboBox.getItems());
        valuesComboBox.getItems().addAll("1000", "2000", "3000","4000");
        valuesComboBox.getSelectionModel().select("1000");
    }

    @FXML
    private void startSearchUsers(ActionEvent event) throws IOException, InterruptedException {

        String searchString = searchWord.getText();
        int numbers =  Integer.parseInt(valuesComboBox.getValue());

        System.out.println(numbers);



            List&lt;Element&gt; result = this.getArrayElementsFirstSearch(searchString);

            if(!result.isEmpty()){



                TaskModalWindow mdc = new TaskModalWindow(event);
                Platform.runLater(mdc);

                TaskGetRetailersURL tsu = new TaskGetRetailersURL (result, numbers, retailerDao);
                Thread t2 = new Thread(tsu);
                t2.start();





            }

    }

    private List&lt;Element&gt; getArrayElementsFirstSearch(String input) throws IOException{

        String searchWordInput = CrawlerUtil.fixSearchWordForUsers(input);
        String inputSearchURL = CrawlerConstants.SEARCH_URL_INPUT_PREFFIX + searchWordInput + CrawlerConstants.SEARCH_URL_INPUT_SUFFIX;
        Document document = Jsoup.connect(inputSearchURL).timeout(10*1000).get();

        List&lt;Element&gt; check =  document.select(CrawlerConstants.SELECTOR_FOR_PAGINATION);
        HeapContainer.firstSetPaginationsAsElement = check;

        return check;


    }


}
</code></pre>

<p>TaskModalWindow.java - Starts the modal window.</p>

<pre><code>package com.mjp.java.threads;

import java.io.IOException;

import com.mjp.java.spring.config.SpringFXMLLoader;

import javafx.event.ActionEvent;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Modality;
import javafx.stage.Stage;

public class TaskModalWindow implements Runnable {

    private ActionEvent event;

    public TaskModalWindow (ActionEvent event) {
        this.event = event;
    }

    private void showProgressBar(ActionEvent event){
        Stage stage = new Stage();
        Parent root;
        try {
            root = FXMLLoader.load(SpringFXMLLoader.class.getResource("/fxml/ProgressWindow.fxml"));
            stage.setScene(new Scene(root));
        } catch (IOException e) {
            e.printStackTrace();
        }
        stage.setTitle("Searching....");
        stage.initModality(Modality.WINDOW_MODAL);
        stage.initOwner(((Node) event.getSource()).getScene().getWindow() );
        stage.show();
    }

    @Override
    public void run() {
        this.showProgressBar(event);

    }
}
</code></pre>

<p>ProgressWindowController.java </p>

<pre><code>package com.mjp.java.controllers;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Task;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;

import org.springframework.stereotype.Component;



@Component
public class ProgressWindowController implements FxmlController {


    @FXML
    private Button buttonStop;

    @FXML
    private ProgressBar progressBar;

    @FXML
    private ProgressIndicator progressIndicator;

    @FXML
    private Text txtState;

    private Task&lt;?&gt; searchWorker;

    public ProgressWindowController (){

    }

    @FXML
    private void onButtonClick() {

    }

    private void changeStateProcessIndicator() {

    }

    public ProgressBar getProgressBar(){
        return progressBar;
    }

    public ProgressIndicator getProgressIndicator(){
        return progressIndicator;
    }

    private Task&lt;?&gt; createSearchTaskItems(int searchedUrls) {
        return new Task&lt;Object&gt;() {

            @Override
            protected Object call() throws Exception {
                while (TaskItemsFeedback.workDoneItems + TaskItemsSold.workDoneItems &lt;= searchedUrls ) {
                    updateProgress(TaskItemsFeedback.workDoneItems+TaskItemsSold.workDoneItems, searchedUrls);
                }
                return true;
            }
        };
    }
    private Task&lt;?&gt; createSearchUserTask(int searchedUrls) {
        return new Task&lt;Object&gt;() {

            @Override
            protected Object call() throws Exception {
                while (TaskFilterRetailers.workDoneUsers &lt;= searchedUrls ) {
                    updateProgress(TaskFilterRetailers.workDoneUsers, searchedUrls);
                }
                return true;
            }
        };
    }

    public void setProgressBarListener(){
        progressBar.indeterminateProperty().addListener(new ChangeListener&lt;Boolean&gt;() {
            @Override
            public void changed(ObservableValue&lt;? extends Boolean&gt; ov, Boolean t, Boolean t1) {
                if (t1) {
                    txtState.setText("Calculating Time");
                } else {
                    txtState.setText("In Progress");

                }
            }
        });
        progressBar.progressProperty().addListener(new ChangeListener&lt;Number&gt;() {


            @Override
            public void changed(ObservableValue&lt;? extends Number&gt; ov, Number t, Number t1) {

                if(t1.doubleValue() == 1){
                    txtState.setText("Work Done");
                    txtState.setFill(Color.GREEN);
                    buttonStop.setDisable(true);
                }
            }

        });

    }

    public void setProgressIndicator(){

        progressIndicator.indeterminateProperty().addListener(new ChangeListener&lt;Boolean&gt;() {

            @Override
            public void changed(ObservableValue&lt;? extends Boolean&gt; ov, Boolean t, Boolean t1) {
                if(t1){
                    txtState.setText("Calculating Time");
                    txtState.setFill(Color.BLUE);
                } else{
                    txtState.setText("In Progress");

                }
            }
        });
    }

    public void startTask() throws InterruptedException{

        while(TaskGetRetailersURL.productsSize == 0 &amp;&amp; TaskItemsStart.allUrlsAndItemsForExecuting==0){
            System.out.println("Items Finder");
        }
        if(TaskGetRetailersURL.productsSize != 0){
            searchWorker = createSearchUserTask(TaskGetRetailersURL.productsSize);
        } else if(TaskItemsStart.allUrlsAndItemsForExecuting!=0){
            searchWorker = createSearchTaskItems((TaskItemsStart.allUrlsAndItemsForExecuting));
        }

        progressBar.progressProperty().unbind();
        progressBar.progressProperty().bind(searchWorker.progressProperty());
        new Thread(searchWorker).start();
    }

    @Override
    public void initialize() {
        progressBar.setProgress(0);
        setProgressBarListener();
        setProgressIndicator();
        startTask();

    }
}
</code></pre>

<p>ProgressWindow.fxml</p>



<pre><code>&lt;?import javafx.scene.control.Button?&gt;
&lt;?import javafx.scene.control.ProgressBar?&gt;
&lt;?import javafx.scene.control.ProgressIndicator?&gt;
&lt;?import javafx.scene.layout.AnchorPane?&gt;
&lt;?import javafx.scene.text.Font?&gt;
&lt;?import javafx.scene.text.Text?&gt;

&lt;AnchorPane prefHeight="201.0" prefWidth="399.0" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.mvp.java.controllers.ProgressWindowController"&gt;
   &lt;children&gt;
      &lt;ProgressBar fx:id="progressBar" layoutX="50.0" layoutY="63.0" prefHeight="12.0" prefWidth="150.0" progress="0.0" /&gt;
      &lt;Button fx:id="buttonStop" alignment="CENTER" contentDisplay="RIGHT" layoutX="314.0" layoutY="126.0" mnemonicParsing="false" onAction="#onButtonClick" prefHeight="25.0" prefWidth="61.0" text="Stop" textAlignment="CENTER" /&gt;
      &lt;ProgressIndicator fx:id="progressIndicator" layoutX="231.0" layoutY="61.0" progress="0.0" /&gt;
      &lt;Text fx:id="txtState" fill="#2708d7" layoutX="274.0" layoutY="79.0" strokeType="OUTSIDE" strokeWidth="0.0" text="In Progress" textAlignment="CENTER" wrappingWidth="101.13671875"&gt;
         &lt;font&gt;
            &lt;Font size="18.0" /&gt;
         &lt;/font&gt;
      &lt;/Text&gt;
   &lt;/children&gt;
&lt;/AnchorPane&gt;
</code></pre>

<p>TaskGetRetailersURL.java </p>

<pre><code>package com.mjp.java.threads;

    import java.io.IOException;


    public class TaskGetRetailersURL implements Runnable {

         public int productsSize = 0;

public TaskGetRetailersURL (List&lt;Element&gt; result, int numberOfPages, RetailerDao retailerDao){
//setting the element
}

        @Override
        public void run() {

            try {
                searchUsers(result, numberOfPages);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
public void searchUsers(List&lt;Element&gt; result, int numberOfPages){
//This method take long time to collect data and initialize productsSize 
}



    }
</code></pre>