Java Reference
In-Depth Information
Calling fill(strings, "Ten"):
list = [Ten, Ten, Ten, Ten]
Change event data:
cursor = 0
Kind of change: replaced
Affected range: [0, 4]
Added size: 4
Added sublist: [Ten, Ten, Ten, Ten]
Removed size: 4
Removed: [Two_1, Three, Four, Five]
Notice that each invocation of a utility method in FXCollections generated exactly one list change event.
Using the JavaFX Concurrency Framework
It is common knowledge nowadays that almost all GUI platforms use a single-threaded event dispatching model.
JavaFX is no exception, and indeed all UI events in JavaFX are processed in the JavaFX Application Thread . However,
with multicore desktop machines becoming common in recent years (e.g., I'm writing this chapter on my quad-core
PC), it is natural for the designers of JavaFX to take advantage of the full power of the hardware by leveraging the
excellent concurrency support of the Java programming language.
In this section, we examine important threads that are present in all JavaFX applications. We explain the role
they play in the overall scheme of JavaFX applications. We then turn our attention to the JavaFX Application Thread,
explaining why executing long-running code in the JavaFX Application Thread makes your application appear to
hang. Finally, we look at the javafx.concurrent framework and show you how to use it to execute long-running code
in a worker thread off the JavaFX Application Thread and communicate the result back to the JavaFX Application
Thread to update the GUI states.
if you are familiar with swing programming, the JavaFX application thread is similar to swing's event
dispatcher thread (edt), usually with the name “aWt-eventQueue-0.”
Note
Identifying the Threads in a JavaFX Application
The program in Listing 7-7 creates a simple JavaFX GUI with a ListView , a TextArea and a Button , and populates
the ListView with the names of all live threads of the application. When you select an item from the ListView , that
thread's stack trace is displayed in the TextArea . The original list of threads and stack traces is populated as the
application is starting up. You can update the list of threads and stack traces by clicking the Update button.
Listing 7-7. JavaFXThreadsExample.java
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
 
 
Search WWH ::




Custom Search