BusyWorker helps running UI tasks a separate threads (other than the JavaFX Application thread). It will show busy cursor and disable specified nodes while task is performed. It gives an option to show progress and status messages. BusyWorker run tasks and takes care of handling handling exceptions and displaying error dialogs. There is also option to perform custom finish actions after task is completed.
While task is performed property busy is set to true. Only one task, for a given worker, can be run at the time. When a task in being performed busyDisabledNode will be disabled and its cursor will be set to Wait/Busy cursor.
Progress and messages from the running task can be monitored using progressValue and progressMessage properties.
Below is an example of using using BusyWorker that updates a progress message and progress indicator. The full example can be found in the BusyWorkerDemo of the ScalaFX Extras Demo project.
val buttonPane: Pane = ...
val progressLabel: Label = ...
val progressBar: ProgressBar = ...
val busyWorker = new BusyWorker("BusyWorker Demo", buttonPane) {
progressLabel.text <== progressMessage
progressBar.progress <== progressValue
}
val button = new Button("Click Me") {
onAction = () => busyWorker.doTask("Task 1")(
new SimpleTask[String] {
override def call(): String = {
val maxItems = 10
for (i <- 1 to maxItems) {
println(i)
message() = s"Processing item $i/$maxItems"
progress() = (i - 1) / 10.0
Thread.sleep(250)
}
progress() = 1
"Done"
}
}
)
}
Attributes
- Companion
- object
- Graph
-
- Supertypes
Members list
Value members
Constructors
Creates a busy worker with a title and nodes to disable when performing tasks. The root node of the parentWindow will be disabled when task is being executed.
Creates a busy worker with a title and nodes to disable when performing tasks. The root node of the parentWindow will be disabled when task is being executed.
The input is a collection of JavaFX or ScalaFX nodes.
val parent: Window = ...
val busyWorker = new BusyWorker("My Task", parent))
Value parameters
- parent
-
window that will be used to display dialogs (if any).
- title
-
title used for unexpected error dialogs.
Attributes
Creates a busy worker with a title and nodes to disable when performing tasks. The root node of the parentWindow will be disabled when task is being executed.
Creates a busy worker with a title and nodes to disable when performing tasks. The root node of the parentWindow will be disabled when task is being executed.
The input is a collection of JavaFX or ScalaFX nodes.
val parent: Option[Window] = ...
val busyWorker = new BusyWorker("My Task", parent))
Value parameters
- parent
-
window that will be used to display dialogs (if any).
- title
-
title used for unexpected error dialogs.
Attributes
Creates a busy worker with a title and nodes to disable when performing tasks. The parent window is the parent window of the node.
Creates a busy worker with a title and nodes to disable when performing tasks. The parent window is the parent window of the node.
The input is a collection of JavaFX or ScalaFX nodes.
val node: scalafx.scene.Node] = ...
val busyWorker = new BusyWorker("My Task", node))
Value parameters
- disabledNode
-
node that will be disabled when performing a task, cannot be null.
- title
-
title used for unexpected error dialogs.
Attributes
Creates a busy worker with a title and nodes to disable when performing tasks. The parent window is the parent window of the first node.
Creates a busy worker with a title and nodes to disable when performing tasks. The parent window is the parent window of the first node.
The input is a collection of JavaFX or ScalaFX nodes.
val nodes = Seq[scalafx.scene.Node] = ...
val busyWorker = new BusyWorker("My Task", nodes))
Value parameters
- disabledNodes
-
nodes that will be disabled when performing a task, if not specified it will be set to root pane of the
parentWindow. - title
-
title used for unexpected error dialogs.
Attributes
Concrete methods
Run a task on a separate thread. Returns immediately (before task is completed). If the task returns a value is can be retrieved through the returned Future.
Run a task on a separate thread. Returns immediately (before task is completed). If the task returns a value is can be retrieved through the returned Future.
Example of running a task without waiting to complete, using a lambda
worker.doTask{ () =>
// Some workload code, does not produce value ot it is discard
Thread.sleep(1000)
print(1 + 1)
}
Example of stating a task (with a lambda) and waiting till it finishes and returns a result
// This code will return before workload is completed
val future = worker.doTask{ () =>
// Some workload code producing final value
Thread.sleep(1000)
1 + 1
}
// This will block till workload competes and the result is retrieved
val result = future.get()
print(result)
Example of running task that updates progress and message, for more details see BusyWorkerDemo.
busyWorker.doTask(
new SimpleTask[String] {
override def call(): String = {
val maxItems = 10
for (i <- 1 to maxItems) {
println(i)
message() = s"Processing item $i/$maxItems"
progress() = (i - 1) / 10.0
Thread.sleep(250)
}
progress() = 1
"Done"
}
}
)
Value parameters
- task
-
actions to perform, can be provided a as a lambda op: => R, see examples above.
Attributes
- Returns
-
Futurethat can be used to retrieve result produced the workload, if any.
Run a task on a separate thread. Returns immediately (before task is completed). If the task returns a value is can be retrieved through the returned Future.
Run a task on a separate thread. Returns immediately (before task is completed). If the task returns a value is can be retrieved through the returned Future.
Example of running a task without waiting to complete, using a lambda
worker.doTask("My Task") { () =>
// Some workload code, does not produce value ot it is discard
Thread.sleep(1000)
print(1 + 1)
}
Example of stating a task (with a lambda) and waiting till it finishes and returns a result
// This code will return before workload is completed
val future = worker.doTask("My Task") { () =>
// Some workload code producing final value
Thread.sleep(1000)
1 + 1
}
// This will block till workload competes and the result is retrieved
val result = future.get()
print(result)
Example of running task that updates progress and message, for more details see BusyWorkerDemo.
busyWorker.doTask("Task 1")(
new SimpleTask[String] {
override def call(): String = {
val maxItems = 10
for (i <- 1 to maxItems) {
println(i)
message() = s"Processing item $i/$maxItems"
progress() = (i - 1) / 10.0
Thread.sleep(250)
}
progress() = 1
"Done"
}
}
)
Value parameters
- name
-
name used for thread that runs the task. May be useful in debugging.
- task
-
actions to perform, can be provided a as a lambda op: => R, see examples above.
Attributes
- Returns
-
Futurethat can be used to retrieve result produced the workload, if any.
Parent window for a dialog. Dialogs are shown modal, the window will be blocked while dialog is displayed.
Parent window for a dialog. Dialogs are shown modal, the window will be blocked while dialog is displayed.
Attributes
- Definition Classes
Inherited methods
Show a confirmation dialog with "OK" and "Cancel" buttons.
Show a confirmation dialog with "OK" and "Cancel" buttons.
Value parameters
- content
-
content text.
- header
-
header text.
- title
-
dialog title.
Attributes
- Returns
-
truewhen user selected 'OK' andfalsewhen user selectedCancelor dismissed the dialog. - Inherited from:
- ShowMessage
Show a confirmation dialog with "OK", "No", and "Cancel" buttons.
Show a confirmation dialog with "OK", "No", and "Cancel" buttons.
Value parameters
- content
-
content text.
- header
-
header text.
- title
-
dialog title.
Attributes
- Returns
-
Some(true)when user selected 'OK',Some(false)when user selectedNo, andNoneuser selectedCancelor dismissed the dialog. - Inherited from:
- ShowMessage
Show error dialog
Show error dialog
Value parameters
- content
-
main content text.
- header
-
header text.
- title
-
dialog title
Attributes
- Inherited from:
- ShowMessage
Displays error dialog with expandable exception information.
Displays error dialog with expandable exception information.
Value parameters
- message
-
Message (excluding t.getMessage(), it is automatically displayed)
- t
-
exception to be displayed in the dialog
- title
-
Dialog title
Attributes
- Inherited from:
- ShowMessage
Show information dialog
Show information dialog
Value parameters
- content
-
main content text.
- header
-
header text.
- title
-
dialog title
Attributes
- Inherited from:
- ShowMessage
Show warning dialog
Show warning dialog
Value parameters
- content
-
main content text.
- header
-
header text.
- title
-
dialog title
Attributes
- Inherited from:
- ShowMessage
Concrete fields
busy property is true when worker is performing a task. Only one task can be done at a time.
busy property is true when worker is performing a task. Only one task can be done at a time.
Attributes
Progress message posted by running task, if any. Current running task's message property is bound to this property (only when task is running).
Progress message posted by running task, if any. Current running task's message property is bound to this property (only when task is running).
Attributes
Progress indicator of a running task, if any, value are between [0 and 1]. Current running task's progress property is bound to this property (only when task is running).
Progress indicator of a running task, if any, value are between [0 and 1]. Current running task's progress property is bound to this property (only when task is running).