0

I am new to JavaFX. I am trying to implement multithreading in my project. Here I want to do download work in a background thread. But in the code, some components like Filechooser and label are not accessed by the background thread, so I used Platform.runLater for it. But then I am stuck in between. Please guide me.

FileChooser fc = new FileChooser();
        FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("Compressed(zipped) Folder (*.zip)", "*.zip");//Compressed(zipped) Folder (*.gz)", "*.gz"
        fc.getExtensionFilters().add(extFilter);
        String downloadsLocation = System.getProperty("user.home") + "/Downloads/";
        /// get a File file with name as you save in file chooser
        Platform.runLater(()->{
            System.out.println("Inside runlator"+Thread.currentThread());
            file = fc.showSaveDialog(downloadMultiTraceFileButton.getScene().getWindow());
            System.out.println("Path of file->" + file.getAbsolutePath());
            file.mkdirs();
 
        });
        System.out.println("after runlator"+Thread.currentThread());
        Thread.sleep(1000);
        System.out.println("after sleep"+Thread.currentThread());

        File theDir = null;
        try{
             theDir = new File(destFile.getAbsolutePath());
        }catch(Exception e){
            System.out.println(e);
        }
        
        /// Iterating through the slected item of trace files one by one for create a file and then fill it
        for (String str : traceFileListView.getSelectionModel().getSelectedItems()) {
            System.out.println("inside of the for loop "+Thread.currentThread());
            File myFile = new File(theDir.getAbsolutePath() + "\\" + str);
            /// make new file if myFile doesnt exist
            if (!myFile.exists()) {
                myFile.createNewFile();
            }

Here backgroung thread is going through it. But this thread is not going to this part of code.

File theDir = null;
        try{
             theDir = new File(destFile.getAbsolutePath());
        }catch(Exception e){
            System.out.println(e);
        }

Please give me solution. I tried every possibility like inserting Thread.currentthread.sleep(1000) and some other things too.

RBT
  • 24,161
  • 21
  • 159
  • 240
  • 2
    [mcve] please .. don't understand why you want to create a FileChooser in a background thread: it's a window meant for interaction with the user, so must be used on the fx application thread anyway .. open the dialog as usual, let the user select a directory/file, then give that selected item to a background thread for reading / writing more files .. – kleopatra May 11 '22 at 12:07
  • 1
    Does this answer your question? [JavaFX periodic background task](https://stackoverflow.com/questions/9966136/javafx-periodic-background-task) – trashgod May 11 '22 at 15:28
  • No , It didn't solved my question. I got my problem solved by using `setOnSuceeded(e_>{})` – abhishek kr sahu May 17 '22 at 14:58

0 Answers0