Anyone who has done GUI development will encounter issues with UI update operations in non-UI threads.
In Android development, you can use asynctask to perform thread operations and UI updates. However, when asynctask is terminated and restarted
We usually use thread and handler to implement relevant functions.
In swing development, we can use swingutilities. invokelater and swingutilities. invokeandwait for processing.
Use swingworker for thread operations and UI updates.
This swingworker is similar to asynctask in Android and implements the same functions.
In javafx, the <t> and service <t> are provided to solve such problems, however, there is no big difference between starting a task using thread and starting a task using service. However, the Service provides restart and other methods to facilitate refresh and other operations.
Example address: http://www.wingmei.net/examples/exp2/
public class taskexample extends Application { public void init(Stage primaryStage){ Region veil = new Region(); veil.setStyle("-fx-background-color: rgba(0, 0, 0, 0.4)"); HBox mHbox = new HBox(10); ProgressIndicator mBar = new ProgressIndicator(0); mBar.setMaxSize(150, 150); Label mLabel = new Label("Loading...0%"); mLabel.setFont(new Font(10)); mHbox.getChildren().add(mBar); mHbox.getChildren().add(mLabel); Task<Void> progressTask = new Task<Void>(){ @Override protected void succeeded() { super.succeeded(); updateMessage("Succeeded"); } @Override protected void cancelled() { super.cancelled(); updateMessage("Cancelled"); } @Override protected void failed() { super.failed(); updateMessage("Failed"); } @Override protected Void call() throws Exception { for(int i = 0; i < 100; i++){ Thread.sleep(50); updateProgress(i + 1, 100); updateMessage("Loading..." + (i + 1) + "%"); } updateMessage("Finish"); return null; } }; StackPane root = new StackPane(); root.getChildren().addAll(veil, mBar, mLabel); Scene scene = new Scene(root, 300, 250); veil.visibleProperty().bind(progressTask.runningProperty()); mBar.progressProperty().bind(progressTask.progressProperty()); mLabel.textProperty().bind(progressTask.messageProperty()); primaryStage.setTitle("The lesson of Task"); primaryStage.setScene(scene); new Thread(progressTask).start(); } @Override public void start(Stage primaryStage) { init(primaryStage); primaryStage.show(); } public static void main(String[] args) { launch(args); }}
The preceding example shows how to use a task.
Region is only a grayscale mask. The visible attribute is bound to the running attribute of the task. When the task is executed, the region mask disappears.
Create a task and perform time-consuming operations in the call method of the task. You can use updateprogress and updatemessage to update the properties of progress and message.
The simplest way to update the UI is to bind the attributes of the UI control with various attributes of the task, so that the UI can be updated synchronously when the task is running the task.
Of course, we can also overwrite methods such as updateprogress and updatemessage for custom operations. This is the same as asynctask in Android.
In addition, javafx provides a tool to package javafx as a local Installation File (Supporting windows, Linux, and Mac). I have used it myself and have time to write and write data.
Reprinted please indicate the source: http://blog.csdn.net/ml3947
Bytes -------------------------------------------------------------------------------------------------
I haven't written a blog for a long time. It may be some work and emotional problems. I haven't made any changes to my blog in the months that have elapsed since January.
Everything has been on the right track recently, so I started to write blogs again.
We hope everyone can work smoothly.