我希望在方法在非fx线程上执行任务时在javafx中显示等待光标。最重要的是,我需要该方法来返回任务的结果(在本例中是一个列表)。我的代码返回空的初始列表,而不是任务的结果。我如何解决它?感谢。
public static final List<String> getCodeList(Scene scene){
List<String> list = new ArrayList();
Task<List<String>> task = new Task<List<String>>() {
@Override
public List<String> call() {
final String CPT_QUERY = "SELECT code, description FROM code_table ORDER BY code";
List<String> codes = new ArrayList();
try (Connection connection = Network.getCurrentConnection();
PreparedStatement getCodes = connection.prepareStatement(CPT_QUERY);) {
ResultSet resultSet = getCodes.executeQuery();
while (resultSet.next()){
codes.add(resultSet.getString("code") + " " + resultSet.getString("description"));
}
} catch (SQLException sqlException) {
sqlException.printStackTrace();
}
return codes;
}
};
scene.setCursor(Cursor.WAIT);
task.setOnSucceeded(e -> {
list.addAll(task.getValue());
scene.setCursor(Cursor.DEFAULT);
});
new Thread(task).start();
return list;
}