-1

I am writing an Application similar to windows' file explorer in Java (JavaFX). The program is just for practising (it is my first ever app). It is a GUI application where you can create/modify/delete files and folders.

There's some buttons that are responsible for sending user actions. Class FileManagerController is a controller class of the main window. I want to add a feature to refresh ListView with all files in current directory in the background. Now it refreshes it only when the user does any action.Here's how this app looks like.

  • 1
    Welcome to SO!! What stops you to start a thread? Can you post some code samples if possible? – miiiii Mar 30 '21 at 20:53
  • 1
    If you are wanting to periodically poll a directory to see changes, read https://stackoverflow.com/questions/9966136/javafx-periodic-background-task/60685975#60685975 You might also want to read the [`WatchService` API](https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/nio/file/WatchService.html) – James_D Mar 30 '21 at 20:55

1 Answers1

0

There's a nice article about this here: https://docs.oracle.com/javafx/2/threads/jfxpub-threads.htm

The highlights are the that "Java FX" way are through the use of a Task and a Service.

You create a Task that has several housekeeping methods for life cycle and such, and you submit it to a Service for execution. The Service will work with a Thread or and Executor Service.

You want to use a Task because it's FX specific. As it says in the documentation:

Because the Task is designed for use with JavaFX GUI applications, it ensures that every change to its public properties, as well as change notifications for state, errors, and for event handlers, all occur on the main JavaFX application thread.

From the article and the documentation, here is a simple example:

 public static class FirstLineService extends Service<String> {
     private StringProperty url = new SimpleStringProperty(this, "url");
     public final void setUrl(String value) { url.set(value); }
     public final String getUrl() { return url.get(); }
     public final StringProperty urlProperty() { return url; }

     protected Task createTask() {
         final String _url = getUrl();
         return new Task<String>() {
             protected String call() throws Exception {
                 URL u = new URL(_url);
                 BufferedReader in = new BufferedReader(
                         new InputStreamReader(u.openStream()));
                 String result = in.readLine();
                 in.close();
                 return result;
             }
         };
     }
 }

Once create, you invoke it with:

service.start();
Will Hartung
  • 115,893
  • 19
  • 128
  • 203
  • AIUI the OP wants to repeatedly poll a folder to update if there are changes. A simple `Service` is probably not the right tool for this job; either a `ScheduledService`, or simply a `Thread` constantly monitoring a `WatchKey` are more likely to be better equipped for this. – James_D Mar 30 '21 at 21:03
  • That's right. I want to monitor changes in actual directory and that is why I used poll with 100 milis. The problem is how to send information to the FX Thread to refresh ListView? – Simo333 Apr 01 '21 at 10:25