public class LazyLoadUtils
extends java.lang.Object
LazyLoadUtils provides an easy way to implement the lazy loading feature in a Combobox or a
ChoiceBox. Sometimes it takes a long time to create the ObservableList for the control. By using this
LazyLoadUtils, we will create the ObservableList only when the popup is about to show or after a delay, so that it
doesn't block the UI thread. The UI will come up without the ObservableList and will be set later.
To use it, simply call one of the install methods. The callback will take care of the creation of the ObservableList.
There are two ways to trigger the callback. The first trigger is when the ComboBox or the ChoiceBox is clicked before the popup content is about to show. The beforeShowing flag will determine if this trigger will be triggered. Default is true. If this trigger is triggered, we will call the callback on the UI thread which means users will have to wait for the callback to finish. We will have to do that because the popup doesn't make sense to show without the ObservableList. The second trigger is triggered after a delay. The delay time is controlled by the delay parameter. When it reaches the specified delay duration, a worker thread will run to call the callback so that it doesn't block the UI. Either trigger can come first but it will be triggered only once. Ideally, when the UI is shown, if user never clicks the ComboBox or the ChoiceBox, the second trigger kicks in and populates the data behind the scene. Not ideally, user clicks on it right away and then he/she has to wait a while. However it is still much better than waiting for the same period of time before the UI showing.
A typical use case is to create a ComboBox that list all the fonts in the system. However the Font.getFamilies call is expensive, especially the system has a lot of fonts. The following code will take care of it.
ComboBox<String> fontComboBox = new ComboBox<>();
fontComboBox.setValue("Arial"); // set a default value without setting the Items
LazyLoadUtils.install(fontComboBox, new Callback<ComboBox<String>, ObservableList<String>>() {
public ObservableList<String> call(ComboBox<String> comboBox) {
return FXCollections.observableArrayList(Font.getFamilies());
}
});
| Constructor and Description |
|---|
LazyLoadUtils() |
| Modifier and Type | Method and Description |
|---|---|
protected static <T> void |
customizeChoiceBox(javafx.scene.control.ChoiceBox<T> choiceBox,
javafx.util.Callback<javafx.scene.control.ChoiceBox<T>,javafx.collections.ObservableList<T>> callback,
javafx.util.Duration delay,
boolean beforeShowing,
T initialValue)
Customizes the ChoiceBox to call the callback until user clicks on the ChoiceBox or after a delay.
|
protected static <T> void |
customizeComboBox(javafx.scene.control.ComboBox<T> comboBox,
javafx.util.Callback<javafx.scene.control.ComboBox<T>,javafx.collections.ObservableList<T>> callback,
javafx.util.Duration delay,
boolean beforeShowing,
T initialValue)
Customizes the ComboBox to call the callback until user clicks on the ComboBox or after a delay.
|
static <T> void |
install(javafx.scene.control.ChoiceBox<T> choiceBox,
javafx.util.Callback<javafx.scene.control.ChoiceBox<T>,javafx.collections.ObservableList<T>> callback)
Customizes the ChoiceBox to call the callback and setItems until user clicks on the ChoiceBox or after a 200 ms
delay.
|
static <T> void |
install(javafx.scene.control.ChoiceBox<T> choiceBox,
javafx.util.Callback<javafx.scene.control.ChoiceBox<T>,javafx.collections.ObservableList<T>> callback,
boolean beforeShowing)
Customizes the ChoiceBox to call the callback and setItems until user clicks on the ChoiceBox (if beforeShowing
is true) or after a 200ms delay.
|
static <T> void |
install(javafx.scene.control.ChoiceBox<T> choiceBox,
javafx.util.Callback<javafx.scene.control.ChoiceBox<T>,javafx.collections.ObservableList<T>> callback,
javafx.util.Duration delay)
Customizes the ChoiceBox to call the callback and setItems until user clicks on the ChoiceBox or after a delay.
|
static <T> void |
install(javafx.scene.control.ChoiceBox<T> choiceBox,
javafx.util.Callback<javafx.scene.control.ChoiceBox<T>,javafx.collections.ObservableList<T>> callback,
javafx.util.Duration delay,
boolean beforeShowing)
Customizes the ChoiceBox to call the callback until user clicks on the ChoiceBox (if beforeShowing is true) or
after a delay.
|
static <T> void |
install(javafx.scene.control.ChoiceBox<T> choiceBox,
javafx.util.Callback<javafx.scene.control.ChoiceBox<T>,javafx.collections.ObservableList<T>> callback,
javafx.util.Duration delay,
boolean beforeShowing,
T initialValue)
Customizes the ChoiceBox to call the callback until user clicks on the ChoiceBox (if beforeShowing is true) or
after a delay.
|
static <T> void |
install(javafx.scene.control.ComboBox<T> comboBox,
javafx.util.Callback<javafx.scene.control.ComboBox<T>,javafx.collections.ObservableList<T>> callback)
Customizes the ComboBox to call the callback and setItems until user clicks on the ComboBox or after a 200 ms
delay.
|
static <T> void |
install(javafx.scene.control.ComboBox<T> comboBox,
javafx.util.Callback<javafx.scene.control.ComboBox<T>,javafx.collections.ObservableList<T>> callback,
boolean beforeShowing)
Customizes the ComboBox to call the callback and setItems until user clicks on the ComboBox (if beforeShowing is
true) or after a 200ms delay.
|
static <T> void |
install(javafx.scene.control.ComboBox<T> comboBox,
javafx.util.Callback<javafx.scene.control.ComboBox<T>,javafx.collections.ObservableList<T>> callback,
javafx.util.Duration delay)
Customizes the ComboBox to call the callback and setItems until user clicks on the ComboBox or after a delay.
|
static <T> void |
install(javafx.scene.control.ComboBox<T> comboBox,
javafx.util.Callback<javafx.scene.control.ComboBox<T>,javafx.collections.ObservableList<T>> callback,
javafx.util.Duration delay,
boolean beforeShowing)
Customizes the ComboBox to call the callback until user clicks on the ComboBox (if beforeShowing is true) or
after a delay.
|
static <T> void |
install(javafx.scene.control.ComboBox<T> comboBox,
javafx.util.Callback<javafx.scene.control.ComboBox<T>,javafx.collections.ObservableList<T>> callback,
javafx.util.Duration delay,
boolean beforeShowing,
T initialValue)
Customizes the ComboBox to call the callback until user clicks on the ComboBox (if beforeShowing is true) or
after a delay.
|
public static <T> void install(javafx.scene.control.ComboBox<T> comboBox,
javafx.util.Callback<javafx.scene.control.ComboBox<T>,javafx.collections.ObservableList<T>> callback)
T - The type of the value that has been selected or otherwise entered in to this ComboBox.comboBox - the ComboBox.callback - the callback to create the ObservableList.public static <T> void install(javafx.scene.control.ComboBox<T> comboBox,
javafx.util.Callback<javafx.scene.control.ComboBox<T>,javafx.collections.ObservableList<T>> callback,
javafx.util.Duration delay)
T - The type of the value that has been selected or otherwise entered in to this ComboBox.comboBox - the ComboBox.callback - the callback to create the ObservableList.delay - the delay before the callback is called.public static <T> void install(javafx.scene.control.ComboBox<T> comboBox,
javafx.util.Callback<javafx.scene.control.ComboBox<T>,javafx.collections.ObservableList<T>> callback,
boolean beforeShowing)
T - The type of the value that has been selected or otherwise entered in to this ComboBox.comboBox - the ComboBox.callback - the callback to create the ObservableList.beforeShowing - whether to trigger the callback before the ComboBox popup is about to show, if the callbackpublic static <T> void install(javafx.scene.control.ComboBox<T> comboBox,
javafx.util.Callback<javafx.scene.control.ComboBox<T>,javafx.collections.ObservableList<T>> callback,
javafx.util.Duration delay,
boolean beforeShowing)
T - The type of the value that has been selected or otherwise entered in to this ComboBox.comboBox - the ComboBox.callback - the callback to create the ObservableList.delay - the delay before the callback is called.beforeShowing - whether to trigger the callback before the ComboBox popup is about to show, if the callback
hasn't called yet.public static <T> void install(javafx.scene.control.ComboBox<T> comboBox,
javafx.util.Callback<javafx.scene.control.ComboBox<T>,javafx.collections.ObservableList<T>> callback,
javafx.util.Duration delay,
boolean beforeShowing,
T initialValue)
T - The type of the value that has been selected or otherwise entered in to this ComboBox.comboBox - the ComboBox.callback - the callback to create the ObservableList.delay - the delay before the callback is called.beforeShowing - whether to trigger the callback before the ComboBox popup is about to show, if the callback
hasn't called yet.initialValue - the initial value. Null if the value has been set on the ComboBox.protected static <T> void customizeComboBox(javafx.scene.control.ComboBox<T> comboBox,
javafx.util.Callback<javafx.scene.control.ComboBox<T>,javafx.collections.ObservableList<T>> callback,
javafx.util.Duration delay,
boolean beforeShowing,
T initialValue)
T - The type of the value that has been selected or otherwise entered in to this ComboBox.comboBox - the ComboBox.callback - the callback to create the ObservableList.delay - the delay before the callback is called.beforeShowing - whether to trigger the callback before the ComboBox popup is about to show, if the callback
hasn't called yet.initialValue - the initial value. Null if the value has been set on the ComboBox.public static <T> void install(javafx.scene.control.ChoiceBox<T> choiceBox,
javafx.util.Callback<javafx.scene.control.ChoiceBox<T>,javafx.collections.ObservableList<T>> callback)
T - The type of the value that has been selected or otherwise entered in to this ChoiceBox.choiceBox - the ChoiceBox.callback - the callback to create the ObservableList.public static <T> void install(javafx.scene.control.ChoiceBox<T> choiceBox,
javafx.util.Callback<javafx.scene.control.ChoiceBox<T>,javafx.collections.ObservableList<T>> callback,
javafx.util.Duration delay)
T - The type of the value that has been selected or otherwise entered in to this ChoiceBox.choiceBox - the ChoiceBox.callback - the callback to create the ObservableList.delay - the delay before the callback is called.public static <T> void install(javafx.scene.control.ChoiceBox<T> choiceBox,
javafx.util.Callback<javafx.scene.control.ChoiceBox<T>,javafx.collections.ObservableList<T>> callback,
boolean beforeShowing)
T - The type of the value that has been selected or otherwise entered in to this ChoiceBox.choiceBox - the ChoiceBox.callback - the callback to create the ObservableList.beforeShowing - whether to trigger the callback before the ChoiceBox popup is about to show, if the
callbackpublic static <T> void install(javafx.scene.control.ChoiceBox<T> choiceBox,
javafx.util.Callback<javafx.scene.control.ChoiceBox<T>,javafx.collections.ObservableList<T>> callback,
javafx.util.Duration delay,
boolean beforeShowing)
T - The type of the value that has been selected or otherwise entered in to this ChoiceBox.choiceBox - the ChoiceBox.callback - the callback to create the ObservableList.delay - the delay before the callback is called.beforeShowing - whether to trigger the callback before the ChoiceBox popup is about to show, if the callback
hasn't called yet.public static <T> void install(javafx.scene.control.ChoiceBox<T> choiceBox,
javafx.util.Callback<javafx.scene.control.ChoiceBox<T>,javafx.collections.ObservableList<T>> callback,
javafx.util.Duration delay,
boolean beforeShowing,
T initialValue)
T - The type of the value that has been selected or otherwise entered in to this ChoiceBox.choiceBox - the ChoiceBox.callback - the callback to create the ObservableList.delay - the delay before the callback is called.beforeShowing - whether to trigger the callback before the ChoiceBox popup is about to show, if the callback
hasn't called yet.initialValue - the initial value. Null if the value has been set on the ChoiceBox.protected static <T> void customizeChoiceBox(javafx.scene.control.ChoiceBox<T> choiceBox,
javafx.util.Callback<javafx.scene.control.ChoiceBox<T>,javafx.collections.ObservableList<T>> callback,
javafx.util.Duration delay,
boolean beforeShowing,
T initialValue)
T - The type of the value that has been selected or otherwise entered in to this ChoiceBox.choiceBox - the ChoiceBox.callback - the callback to create the ObservableList.delay - the delay before the callback is called.beforeShowing - whether to trigger the callback before the ChoiceBox popup is about to show, if the callback
hasn't called yet.initialValue - the initial value. Null if the value has been set on the ChoiceBox.