0

I am developing a javafx desktop application. The app is reading images from a directory then resize them and save related data to the database. Because the there are 10000 images, so I run the code inside a Service. Everything works fine until the images are all inside one directory (no sub-direcotries). When there are sub-directories the progress bar gets vanished as soon as the cursor reads a sub-directory and also the following error is desplayed in console. Any adeas?

public void startProcessing(String dirPath) {
    Service service = new Service() {
        @Override
        protected Task createTask() {
            return new Task() {
                @Override
                protected Object call() {
                    File dir = new File(dirPath);
                    for (final File f : dir.listFiles()) {
                        if (f.isFile()) {
                            BufferedImage img = null;
                            try {
                                img = ImageIO.read(f);
                                imgImpl.create(f.getName(), f.getPath(), f.length(),
                                        img.getHeight(), img.getWidth(),
                                        new Date(f.lastModified())
                                updateProgress(count, totalNumberOfFiles);
                                resizeImages(f.getPath(), f.getName());
                                count++;
                            } catch (final Exception e) {
                                updateProgress(count, totalNumberOfFiles);
                                count++;
                            }
                        } else if (f.isDirectory()) {
                            startProcessing(f.getAbsolutePath());
                        }
                        progressbar.setVisible(false);
                    }
                    return null;
                }
            };
        }
    };
    progressbar.setVisible(true);
    progressbar.progressProperty().bind(service.progressProperty());
    service.start();
}

Jun 24, 2019 4:36:39 PM javafx.concurrent.Service lambda$static$1 WARNING: Uncaught throwable in javafx concurrent thread pool java.lang.IllegalStateException: Not on FX application thread; currentThread = Thread-4

Rafa
  • 467
  • 4
  • 18
  • You shouldn't change the visibility of the `ProgressBar` on the background thread. Furthermore doing this every loop iteration makes even less sense. Move this logic to `onSucceeded`(and possibly `onFailed`) event handler... – fabian Jun 24 '19 at 11:43
  • Thanks for the reply!You suggested the best way to handle the visibility of the progress bar. But still the problem exists. As soon as the the else if block is executed the same Exception is given and the progress bar disappears! I have added this line before service.start(). service.setOnSucceeded(e-> progressbar.setVisible(false)); – Rafa Jun 24 '19 at 12:07

0 Answers0