JavaFx - GUI Update

All I need to do is update the shortcut as my program launches. I am reading several files and I wanted it to display the name of the file that it was reading.

However, it only displays the last file using the following code (basically the GUI does not respond until the whole process is complete):

static Text m_status_update = new Text(); //I declared this outside the function so dont worry m_status_update.setText("Currently reading " + file.getName()); 

I have about 4-5 files, and I just want to display that name.

I saw a similar question Displaying changing values ​​in a JavaFx shortcut , the best answer recommended the following:

 Label myLabel = new Label("Start"); //I declared this outside the function so dont worry myLabel.textProperty().bind(valueProperty); 

However, valueProperty is StringProperty, and I am stuck in converting a string to a string property.

Also, I saw this Update shortcut in JAVAFX , but the OP could update the label based on the action. I really have no action?

+8
java javafx
source share
1 answer

If you run the whole process in the FX application thread, then this is (in fact) the same thread that is used to display the user interface. If both the display of the user interface and the process of iterating the file are performed in one thread, only one can happen at once. Thus, you are forbidden to update the user interface until the process is completed.

Here is a simple example where I just pause 250 milliseconds between each iteration (simulating reading a large enough file). One button launches this in the FX application thread (note that the user interface does not respond to requests at runtime - you cannot enter a text field). Another button uses Task to run in the background, correctly scheduling updates for the user interface in the FX application thread.

 import javafx.application.Application; import javafx.concurrent.Task; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.stage.Stage; public class UpdateTaskDemo extends Application { @Override public void start(Stage primaryStage) { Label label = new Label(); Button runOnFXThreadButton = new Button("Update on FX Thread"); Button runInTaskButton = new Button("Update in background Task"); HBox buttons = new HBox(10, runOnFXThreadButton, runInTaskButton); buttons.setPadding(new Insets(10)); VBox root = new VBox(10, label, buttons, new TextField()); root.setPadding(new Insets(10)); runOnFXThreadButton.setOnAction(event -> { for (int i=1; i<=10; i++) { label.setText("Count: "+i); try { Thread.sleep(250); } catch (InterruptedException exc) { throw new Error("Unexpected interruption"); } } }); runInTaskButton.setOnAction(event -> { Task<Void> task = new Task<Void>() { @Override public Void call() throws Exception { for (int i=1; i<=10; i++) { updateMessage("Count: "+i); Thread.sleep(250); } return null ; } }; task.messageProperty().addListener((obs, oldMessage, newMessage) -> label.setText(newMessage)); new Thread(task).start(); }); primaryStage.setScene(new Scene(root, 400, 225)); primaryStage.show(); } public static void main(String[] args) { launch(args); } } 
+15
source share

All Articles