我的UI之外,并希望显示等待光标东西正在发生的事情,并使用这个基本模式:
on UI - primaryStage.scene.cursor = Cursor.WAIT try { do stuff off UI... } finally { on UI - primaryStage.scene.cursor = Cursor.DEFAULT }
在运行,我可以启动另一个过程,很快完成并第一个任务完成之前光标被恢复。
我不介意“等待”,而第一个任务完成,但我不认为这意味着做UI线程上的工作? 是否有在JavaFX中提供这种模式的任何内置的解决方案?
- 我的标签包含2组合框。 当我打第二个组合框下拉,等待光标有时会出现过,即使当前光标默认状态列表。 如果我外面将鼠标指针移动/后退清单上的光标正确显示为默认。 这将是一个单独的问题或某种联系?
视图
label 'From'
comboBox(items: bind(model.wcomboFromItemsProperty()), value: bind(model.wcomboFromProperty()), selectFromAction)
label 'To'
comboBox(items: bind(model.wcomboFromItemsProperty()), value: bind(model.wcomboToProperty()), selectToAction)
模型
@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
//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)
}
其他
@EqualsAndHashCode(includes = 'textValue')
class ListElement implements Serializable {
String textValue = ""
Serializable objectValue // Serializable object from business model
@Override
String toString() {
textValue
}
}
格里芬框架自动调用UI线程以外的OnAction控制器事件。 GroovyFX含有一些神奇的它增加势必selectionModel.selectedItemProperty即一个“ONSELECT”行动
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);
}
...
}
}