管理任务的等待光标


  1. 我在UI之外,希望在东西被打开时显示一个等待光标发生并使用这个基本模式:

    on UI - primaryStage.scene.cursor = Cursor.WAIT try { do stuff off UI... } finally { on UI - primaryStage.scene.cursor = Cursor.DEFAULT }

在运行时,我可以启动另一个进程,该进程很快完成,并且在第一个任务完成之前恢复Cursor。

我不介意在第一个任务完成时"等待",但我不认为这意味着在UI线程上完成工作?javafx中是否提供了此模式的内置解决方案?

  1. 我的选项卡包含2个组合框。当我点击第二个组合框下拉菜单时,WAIT光标有时会出现在列表上,即使光标当前处于DEFAULT状态。如果我在列表上向外/向后移动鼠标指针,光标将正确显示为默认值。这是一个单独的问题还是有某种关联

查看

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)

控制器

//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
    }
}

Griffon框架在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);
        }
        ...
    }
}

javafx中是否提供了此模式的内置解决方案?

我建议您使用内置的Task;(

它有预定义的方法来处理您需要的一切。

private Task<Void> backgroundTask = new Task() {
    @Override
    protected Void call() throws Exception {
        // Something to do on background thread ;
        return null;
    }
};

它有一个runningProperty((,它可以绑定到场景的cursorProperty((。

您可以创建两个包含Cursor.DEFAULTCURSOR.WAITObjectProperty<Cursor>

final ObjectProperty<Cursor> CURSOR_DEFAULT = new SimpleObjectProperty<>(Cursor.DEFAULT);
final ObjectProperty<Cursor> CURSOR_WAIT = new SimpleObjectProperty<>(Cursor.WAIT);

然后您可以将它们绑定到任务:

scene.cursorProperty().bind(Bindings.when(backgroundTask.runningProperty())
                                            .then(CURSOR_WAIT).otherwise(CURSOR_DEFAULT));

这是一个单独的问题还是有某种关联?

如果您在ComboBox上的操作以某种方式调用了后台线程,那么它可能是相关的,否则很难进行注释。

您也可以使用griffon-tasks-pluginhttp://griffon-plugins.github.io/griffon-tasks-plugin/

该插件提供了一个UI工具包agnostik SwingWorker-like API,用于在后台线程中执行任务。

最新更新