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.