Skip to content
Advertisement

JavaFX Multithreading and Progressbar

I have a problem with freeze GUI. I’m a beginner with JavaFX and don’t know what I’m doing wrong. VideoToImages is background method from which I’m getting IntegerProperties to set progressBar value.

This is my code:

public class FXMLDocumentController implements Initializable {

Service thread;
private IntegerProperty proc;
private IntegerProperty prom;
@FXML
private Label output;
@FXML
private Label source;
@FXML
private CheckBox color;
@FXML
private ProgressBar statusbar;
   
@FXML
public void check()
{
    ...
    
    Stage st=new Stage();
    Task ta =new Task() {
        @Override
        protected Object call() throws Exception {
               VideoToImages.start(st,proc,prom,color.isSelected(),source.getText(),output.getText());
               updateProgress(proc.longValue(), prom.longValue());
            return null;
        }
    };
    
     Thread te=new Thread(ta);
     statusbar.progressProperty().bind(ta.progressProperty());
     te.run();
}

}
@Override
public void initialize(URL url, ResourceBundle rb) {
   
    proc=new SimpleIntegerProperty();
    prom=new SimpleIntegerProperty();

}    
}

VideoToImages.start is getting frames from video and changes them to ascii images, saves this frames as images and put them to video. This is part where images are processing, prom is set and proc updatting.

public static void start(Stage primaryStage, IntegerProperty proc,IntegerProperty prom, boolean kolor, String source, String output)  { 

  mp.setOnEndOfMedia(()->{
    primaryStage.hide();
   ImagesToVideo vidSaver=new ImagesToVideo(saveDir, licz, klatka);
    
    int max=fd.listFiles().length;
    proc.setValue(0);
    prom.setValue(max);
    int pro=0;
    for(File fi: fd.listFiles())
    {         
      try {
          pro++;
          BufferedImage tempImg=ImageIO.read(fi);
          tempImg=ImageToAscii.CharToIMG3(tempImg, kolor);                                                   
          ImageIO.write(tempImg, "jpg", fi);
          
          proc.setValue(pro);
          
      } catch (IOException ex) {
          Logger.getLogger(VideoToImages.class.getName()).log(Level.SEVERE, null, ex);
      }
    }  

...

  });       

}

Advertisement

Answer

I finally got this. I sliced processing function to few smaller and separated from gui. Now i get progress from task property. Anyway thanks for help. This is test code:

   //Main function
        VideoToImages vti=new VideoToImages();
        Task ta=vti.getT();
         Thread te=new Thread(ta);
         statusbar.progressProperty().bind(ta.progressProperty());
         te.start();

    //Task class
   public class VideoToImages {
        public static Task t;

        public VideoToImages() {
       t=new Task() {
                @Override
                protected Object call() throws Exception {
                  // prom.setValue(100);
                      for(int i=0; i<100; i++)
                        {
                           // proc.setValue(i);
                            updateProgress(i, 100);
                            Thread.sleep(100);
                        }
                    return null;
                }
            };
        }

        public Task getT() {
            return t;
        }


    }
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement