Java Reference
In-Depth Information
Chapter 7
Collections and Concurrency
When you know a thing, to hold that you know it; and when you do not know a thing, to allow that
you do not know it;—this is knowledge.
—Confucius
After the fast-paced exploration of JavaFX layouts in Chapter 5 and JavaFX UI controls in Chapter 6, we refocus our
attention on some of the lower level facilities of JavaFX in this chapter.
Recall that in Chapter 4 you learned about the Observable interface and one of its subinterfaces
ObservableValue . In this chapter, we examine four other subinterfaces of Observable ObservableList ,
ObservableMap , ObservableSet , and ObservableArray —rounding out the story of the Observable family of interfaces
and classes.
We then cover concurrency in JavaFX. We explain the JavaFX threading model, pointing out the most important
threads present in a JavaFX application. We look at the rules that you must follow to ensure your JavaFX application is
responsive to user inputs and not locked up by event handlers that take too long to execute. We also show you how the
javafx.concurrent framework can be used to offload long-running routines to background threads.
We conclude this chapter with examples that show how a JavaFX scene graph can be embedded into a Swing
application using JFXPanel , how it can be embedded into an SWT application using FXCanvas , paying attention
to how to make the JavaFX event thread play nicely with the Swing event dispatching thread, and how Swing
components can be embedded into a JavaFX scene using SwingNode .
Understanding Observable Collections and Arrays
As we saw in Chapter 4, the Observable interface has five direct subinterfaces—the ObservableValue interface, the
ObservableList interface, the ObservableMap interface, the ObservableSet interface, and the ObservableArray interface.
We learned that the ObservableValue interface plays a central role in the JavaFX Properties and Bindings framework.
The ObservableList , ObservableMap , ObservableSet , and ObservableArray interfaces reside in the javafx.
collections package, and are referred to as the JavaFX observable collections and arrays. In addition to extending
the Observable interface, ObservableList , ObservableMap , and ObservableSet al so extend the java.util.
List , java.util.Map , and java.util.Set interfaces, respectively, making them genuine collections in the eyes of
the Java collections framework. You can call all the Java collections framework methods you are familiar with on
objects of these interfaces and expect exactly the same results. What the JavaFX observable collections and arrays
provide in addition to the stock Java collections framework are notifications to registered listeners. Because they are
Observable s, you can register InvalidationListener s with the JavaFX observable collections and arrays objects and
be notified when the content of the observable collections and arrays becomes invalid.
 
Search WWH ::




Custom Search