I'm here for the third time, this time about the threading within JavaFX.
So, I'm trying to have a loading screen, and the loadingscreen has a controller which is used as interface (NOT ACTUAL INTERFACE) between the entry and the result.
Here is the code:
private void createLoadingScreenScene(String id) throws Exception {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/com/lollookup/scene/loadingscreen/loadingscreen.fxml"));
Stage stage = new Stage(StageStyle.DECORATED);
stage.setScene(new Scene(loader.load()));
stage.show();
Task task = new Task<Void>() {
@Override
protected Void call() throws Exception {
LoadingScreenController controller = loader.getController();
controller.setSummoner(lookupTextField.getText(), regionsBox.getSelectionModel().getSelectedItem());
switch (id.toLowerCase()) {
case "ag_lookup":
controller.loadActiveGame();
break;
case "sum_lookup":
controller.loadProfile();
break;
}
return null;
}
};
ExecutorService executorService = Executors.newSingleThreadExecutor();
Future<?> submit = executorService.submit(task);
}
everything works perfectly fine until this: assuming the id is sum_lookup this method gets executed:
public void loadProfile() {
if (summoner != null) {
try {
System.out.println("Loading profile scene");
createProfileScene();
} catch (DataException | WrongRequestFormatException | ReplyException | IOException e) {
e.printStackTrace();
}
}
}
And createProfileScene is the following:
private void createProfileScene() throws IOException, DataException, WrongRequestFormatException, ReplyException {
System.out.println("Going ham..");
String summonerName = summoner.getName();
System.out.println("Going ham..0.5");
FXMLLoader loader = new FXMLLoader(getClass().getResource("/com/lollookup/scene/summonerlookup/profile.fxml"));
System.out.println("Going ham.. 0.8");
Stage stage = new Stage(StageStyle.DECORATED);
System.out.println("Going ham..1");
stage.setScene(new Scene(loader.load()));
ProfileController controller = loader.getController();
List<ChampionMastery> championMasteries = leagueAPI.getChampionMasteries(summoner.getId()); //TODO here
System.out.println("Going ham..2");
stage.show()
Now everything works until "Going ham 0.8" is being printed. The program literally stops outputting data.
That's the output:
Loading profile scene
Going ham..
Going ham..0.5
Going ham.. 0.8
What am i doing wrong?
EDIT:
Thats the code I now changed: private void createLoadingScreenScene(String id) throws Exception { FXMLLoader loader = new FXMLLoader(getClass().getResource("/com/lollookup/scene/loadingscreen/loadingscreen.fxml")); Stage stage = new Stage(StageStyle.DECORATED); stage.setScene(new Scene(loader.load())); LoadingScreenController controller = loader.getController(); controller.setSummoner(lookupTextField.getText(), regionsBox.getSelectionModel().getSelectedItem()); switch (id.toLowerCase()) { case "ag_lookup": controller.loadActiveGame();
case "sum_lookup":
controller.loadProfile();
}
stage.show();
}
This is the actual task which takes heavy load of time: (now in a Task)
Task task = new Task<Void>() {
@Override
protected Void call() throws Exception {
System.out.println("Going ham..");
String summonerName = summoner.getName();
System.out.println("Going ham..0.5");
FXMLLoader loader = new FXMLLoader(getClass().getResource("/com/lollookup/scene/summonerlookup/profile.fxml"));
System.out.println("Going ham.. 0.8");
Stage stage = new Stage(StageStyle.DECORATED);
System.out.println("Going ham..1");
stage.setScene(new Scene(loader.load()));
ProfileController controller = loader.getController();
List<ChampionMastery> championMasteries = leagueAPI.getChampionMasteries(summoner.getId()); //TODO unused
System.out.println("Going ham..2");
ChampionInfoData[] championStatsSummary = leagueAPI.getChampionStatsRanked(summoner.getId(), Season.SEASON_7).getChampionStatsSummary().stream().filter(element -> element.getId() != 0).map(element -> {
try {
ChampionStats championStats = element.getChampionStats();
int championId = element.getId();
return new ChampionInfoData(leagueAPI.getImageUrl(championId), leagueAPI.getChampionData(championId).getName(), championStats.displayAverageKDA(), championStats.displayWinrate(), championStats.displayAverageCreepScore(), "0");
} catch (ReplyException | DataException | IOException | WrongRequestFormatException e) {
e.printStackTrace();
}
return null;
}).toArray(ChampionInfoData[]::new);
System.out.println("Did i come this far? Yes!" + championStatsSummary.length);
controller.createProfile(new SummonerData("http://avatar.leagueoflegends.com/" + summoner.getRegion().getShortCode() + "/" + summonerName.replace(" ", "") + ".png", summonerName, String.valueOf(summoner.getSummonerLevel())), championStatsSummary);
stage.show();
return null;
}
};
ExecutorService executorService = Executors.newSingleThreadExecutor();
executorService.submit(task);
Yet the output still is:
Loading profile scene
Going ham..
Going ham..0.5
Going ham.. 0.8