我试图通过在单独的线程上进行工作并返回所需的对象来对JavaFX中的GUI进行更改。但是,在完成工作和task.setOnSucceeded()之后,我尝试检索创建的对象并得到错误“不兼容的类型:对象无法转换为VideoScrollPane类型”。
我认为这与原始类型有关,因为它发生在侦听器中,但是四处查看后,我找不到我想要的建议。
任何可以散发出的光将不胜感激。
Task task = new Task() {
VideoScrollPane vsp;
@Override protected VideoScrollPane call() {
try {
System.out.print("thread...");
ExecutorService executor = Executors.newCachedThreadPool();
Future future = executor.submit(new Callable() {
@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() {
@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()); }
}
});