管理任务的等待光标 [英] Manage wait cursor for task
问题描述
-
我在UI之外,希望显示一个等待光标,而东西是
发生并使用这个基本模式:
I'm outside the UI and wish to display a wait cursor while stuff is happening and using this basic pattern:
- primaryStage.scene.cursor = Cursor.WAIT
尝试{
关闭UI ...
}最终{$ b $ UI上的b - primaryStage.scene.cursor = Cursor.DEFAULT
}
运行时我可以启动另一个快速完成的进程,并在第一个任务完成之前恢复Cursor。
While running I can start another process which completes quickly and the Cursor is restored before the first task completes.
我不介意等待第一个任务完成,但我不认为这意味着在UI线程上工作?
javafx中是否提供此模式的内置解决方案?
I don't mind "waiting" while the first task completes, but I don't think this means doing the work on the UI thread? Is there any built in solution for this pattern provided in javafx?
- 我的标签包含2个组合框。当我按下第二个组合框下拉时,即使光标当前处于默认状态,有时也会在列表上显示WAIT光标。如果我在列表的外侧/后侧移动鼠标指针,则光标将正确显示为默认值。这是一个单独的问题还是以某种方式相关?
查看
label 'From'
comboBox(items: bind(model.wcomboFromItemsProperty()), value: bind(model.wcomboFromProperty()), selectFromAction)
label 'To'
comboBox(items: bind(model.wcomboFromItemsProperty()), value: bind(model.wcomboToProperty()), selectToAction)
MODEL
@FXObservable ListElement wcomboFrom = new ListElement()
@FXObservable ListElement wcomboTo = new ListElement()
@FXObservable List wcomboFromItems = FXCollections.observableArrayList()
@FXObservable List wcomboToItems = FXCollections.observableArrayList()
final ObjectProperty<Cursor> CURSOR_DEFAULT = new SimpleObjectProperty<>(Cursor.DEFAULT)
final ObjectProperty<Cursor> CURSOR_WAIT = new SimpleObjectProperty<>(Cursor.WAIT)
CONTROLLER
CONTROLLER
//lifecycle
void onReadyStart(GriffonApplication application) {
loadWindowData()
}
// both combo boxes contain the same items
protected void loadWindowData() {
def list = [new ListElement(textValue: '')]
list.addAll dataService.getData().collect {
new ListElement(textValue: it.name, objectValue: it)
}
runInsideUIAsync {
model.wcomboFromItems.addAll(list)
model.wcomboToItems.addAll(list)
}
}
void selectFrom() {
performAction {
gcListFrom = getControlList(model.wcomboFrom.objectValue)
setTreeItems(model.wtreeGcFrom, gcListFrom, model.wcomboFrom)
setTreeItems(model.wtreeGcTo, gcListTo, model.wcomboTo)
}
}
void selectTo() {
performAction {
gcListTo = getControlList(model.wcomboTo.objectValue)
setTreeItems(model.wtreeGcTo, gcListTo, model.wcomboTo)
}
}
def performAction = {c ->
Task<Void> t = new Task() {
@Override protected Void call() {
println "Running closure " + isUIThread()
c.call()
}
}
runInsideUISync {
application.primaryStage.scene.cursorProperty().bind(Bindings.when(t.runningProperty())
.then(model.CURSOR_WAIT).otherwise(model.CURSOR_DEFAULT))
}
runOutsideUI(t)
}
OTHER
@EqualsAndHashCode(includes = 'textValue')
class ListElement implements Serializable {
String textValue = ""
Serializable objectValue // Serializable object from business model
@Override
String toString() {
textValue
}
}
Griffon框架自动调用UI线程外的onAction控制器事件。 GroovyFX包含一些魔法,它添加了一个绑定到selectionModel.selectedItemProperty的onSelect动作,即
The Griffon framework automatically invokes the onAction controller events outside the UI thread. GroovyFX contains some magic which adds an "onSelect" action bound to selectionModel.selectedItemProperty i.e.
class GroovyFXEnhancer {
static void enhanceClasses() {
...
ComboBox.metaClass {
cellFactory << { Closure closure -> delegate.setCellFactory(closure as Callback)}
onSelect << { Closure closure ->
delegate.selectionModel.selectedItemProperty().addListener(closure as ChangeListener);
}
...
}
}
推荐答案
javafx中是否有针对此模式的内置解决方案?
Is there any built in solution for this pattern provided in javafx?
我建议你使用内置的任务;)
I would advice you to use the built in Task ;)
它有预定义的方法来处理你需要的一切。
It has predefined methods to handle everything you need.
private Task<Void> backgroundTask = new Task() {
@Override
protected Void call() throws Exception {
// Something to do on background thread ;
return null;
}
};
它有 runningProperty(),它可以绑定到 cursorProperty()。
您可以创建两个 ObjectProperty< Cursor>
,其中包含 Cursor.DEFAULT
和 CURSOR。等待
。
You can create two ObjectProperty<Cursor>
containing Cursor.DEFAULT
and CURSOR.WAIT
.
final ObjectProperty<Cursor> CURSOR_DEFAULT = new SimpleObjectProperty<>(Cursor.DEFAULT);
final ObjectProperty<Cursor> CURSOR_WAIT = new SimpleObjectProperty<>(Cursor.WAIT);
然后你可以将它们绑定到任务:
Then you can bind them to the task :
scene.cursorProperty().bind(Bindings.when(backgroundTask.runningProperty())
.then(CURSOR_WAIT).otherwise(CURSOR_DEFAULT));
这是一个单独的问题还是以某种方式相关?
Would this be a separate issue or somehow related?
如果您对ComboBox的操作以某种方式调用后台线程,那么它可能是相关的,否则很难发表评论。
If your action on the ComboBox is somehow invoking the background thread, then it might be related, else it is difficult to comment.
这篇关于管理任务的等待光标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!