【发布时间】:2015-05-15 01:11:36
【问题描述】:
我正在尝试通过在单独的线程上进行工作并返回所需的对象来更改我在 JavaFX 中的 GUI。但是,在完成工作并触发 task.setOnSucceeded() 后,我尝试检索创建的对象并收到错误“不兼容的类型:对象无法转换为类型 VideoScrollPane”。
我认为这与原始类型有关,因为它在侦听器中正在发生,但环顾四周后我找不到我正在寻找的建议。
我们将不胜感激。
Task task = new Task<VideoScrollPane>() {
VideoScrollPane vsp;
@Override protected VideoScrollPane call() {
try {
System.out.print("thread...");
ExecutorService executor = Executors.newCachedThreadPool();
Future<VideoScrollPane> future = executor.submit(new Callable<VideoScrollPane>() {
@Override public VideoScrollPane call() {
return new VideoScrollPane(mediaview, vboxCentre, username, project);
}
});
vsp = future.get();
} catch(Exception exception) { System.out.println(exception.getMessage()); }
return vsp;
}
};
new Thread(task).start();
task.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
@Override public void handle(WorkerStateEvent t) {
System.out.println("complete");
try {
//where the problem occurs
VideoScrollPane v = task.get();
} catch(Exception exception) { System.out.println(exception.getMessage()); }
}
});
【问题讨论】:
-
当你说“得到错误”时,我假设你的意思是编译错误而不是运行时错误。你试过投射吗?
-
是的,编译错误,抱歉。