【发布时间】:2016-03-22 21:10:32
【问题描述】:
我有一个接口方法,它应该返回一个 Future 对象。
Future<Result> doSomething()
这个方法的实现显示了一些ui(javafx)。 其中一个 ui 元素有一个侦听器,需要调用它才能接收实际结果,我需要。
我如何实现这一目标? 有没有更好的解决方案?
这是我需要等待的示例操作:
// this is some framework method I cannot change
@Override
public Data execute(Data data) {
Future<Data> dataFuture = handler.doSomething(data);
// this should basically wait until the user clicked a button
return dataFuture.get();
}
// handler implementation
public Future<Data> doSomething(Data data) {
// the question is how to implement this part, to be able to
// return a future object
Button button = new Button("Wait until click");
// create thread that waits for the button click ?!????
// modify incoming data object when the button was clicked
// somehow create the Future object that's bound to the button click
return future;
}
这就是我想要实现的目标:
- 我的方法 doSomething 显示了一个带有按钮的新场景 (ui)
- 并立即返回未来对象
- future.get() 等待用户按下按钮
限制:必须在没有额外库的情况下使用 >=Java7
【问题讨论】:
-
从哪里调用这个方法? (FX 应用程序线程还是后台线程?)为什么需要以这种方式做事(即为什么不使用通常的事件驱动机制来“在按下按钮时执行某事”)?按钮将显示在哪里?在对话框中?在现有阶段?您可能需要提供 许多 更多详细信息,或者最好提供一个完整的可执行示例,以阐明您正在尝试做什么。
标签: java javafx futuretask