1

I'm developing an app using JavaFX. In app I'm switching to another scene over clicking a button in another one. What I want to do is when I got back to first scene I want it to keep all properties assigned to the controls. Is it possible to save instance state and then load it when needed like in android?

This is Main Scene controller :

public class MainController {

@FXML
Button go;
@FXML
TextField summName;
@FXML
public ImageViewController champ1;
@FXML
public ImageViewController champ2;
@FXML
public ImageViewController champ3;
@FXML
public ImageViewController champ4;
@FXML
public ImageViewController champ5;
@FXML
public ImageViewController champ6;
@FXML
public ImageViewController champ7;
@FXML
public ImageViewController champ8;
@FXML
public ImageViewController champ9;
@FXML
public ImageViewController champ10;
@FXML
public GridPane gridPane;
@FXML
public ProgressBar progress;

public static Participant participant;
public static boolean isLoaded = false;
private CurrentGameInfo game;
public static DownloadTask downloadTask;

public void initialize() throws URISyntaxException {
    BackgroundImage backgroundImage = new BackgroundImage(new Image(MainController.class.getClassLoader().getResource("img/bg.png").toURI().toString()), null, null, null, new BackgroundSize(1920, 1080, false, false, false, false));
    Background background = new Background(backgroundImage);
    gridPane.setBackground(background);

}

@FXML
public void onAction() throws IOException {
    downloadTask = new DownloadTask();

    progress.progressProperty().unbind();
    progress.progressProperty().bind(downloadTask.progressProperty());
    new Thread(downloadTask).start();

    downloadTask.addEventHandler(WorkerStateEvent.WORKER_STATE_SUCCEEDED, (event) -> {
        progress.progressProperty().unbind();
        progress.setProgress(100);
    });
}

private void changeScene() throws IOException {

    LolAppApplication.mainStage.getScene().setRoot(FXMLLoader.load(getClass().getClassLoader().getResource("secondscene.fxml")));
}

private class DownloadTask extends Task<Void> {

    @Override
    protected Void call() throws Exception {
        if (!isLoaded) {
            game = CurrentGameInfoUtils.getCurrentGameInfo(summName.getText(), Region.TR);
            isLoaded = true;
        }
        List<Participant> participants = game.getCurrentGameParticipants();
        List<Image> imagesTeam1 = new ArrayList<>();
        List<Image> imagesTeam2 = new ArrayList<>();
        List<Champion> champions = new ArrayList<>();
        List<String> participantNames = new ArrayList<>();

        for (Participant p1 : participants) {
            System.out.println(ChampionUtils.getChampion((int) p1.getChampionId()).getName());
            List<Long> perkIds = p1.getPerks().getPerkIds();
            perkIds.forEach((perkId) -> {
                try {
                    Rune rune = RuneUtils.getRune(Integer.valueOf(String.valueOf(perkId)));
                    System.out.println(rune.getName() + " " + rune.getRunePathName() + " " + rune.getShortDesc());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });
            System.out.println(p1.getPerks().getPerkStyle());
            System.out.println(p1.getPerks().getPerkSubStyle());
            System.out.println(SpellUtils.getSpell((int) p1.getSpell1Id()).getName());
            System.out.println(SpellUtils.getSpell((int) p1.getSpell2Id()).getName());
            System.out.println(p1.getSummonerName());

            System.out.println(ChampionUtils.getChampion((int) p1.getChampionId()).getSplash());
            File file = new File(ChampionUtils.getChampion((int) p1.getChampionId()).getSplash());
            Image image = new Image(file.toURI().toString());
            if (p1.getTeamId() == 100)
                imagesTeam1.add(image);
            else
                imagesTeam2.add(image);
            champions.add(ChampionUtils.getChampion((int) p1.getChampionId()));
            participantNames.add(p1.getSummonerName());
        }

        champ1.setImage(imagesTeam1.get(0));
        champ2.setImage(imagesTeam1.get(1));
        champ3.setImage(imagesTeam1.get(2));
        champ4.setImage(imagesTeam1.get(3));
        champ5.setImage(imagesTeam1.get(4));
        champ6.setImage(imagesTeam2.get(0));
        champ7.setImage(imagesTeam2.get(1));
        champ8.setImage(imagesTeam2.get(2));
        champ9.setImage(imagesTeam2.get(3));
        champ10.setImage(imagesTeam2.get(4));

        champ1.setParticipant(participants.get(0));
        champ2.setParticipant(participants.get(1));
        champ3.setParticipant(participants.get(2));
        champ4.setParticipant(participants.get(3));
        champ5.setParticipant(participants.get(4));
        champ6.setParticipant(participants.get(5));
        champ7.setParticipant(participants.get(6));
        champ8.setParticipant(participants.get(7));
        champ9.setParticipant(participants.get(8));
        champ10.setParticipant(participants.get(9));

        champ1.setOnMouseClicked((event -> {
            try {
                participant = champ1.getParticipant();
                changeScene();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }));
        champ2.setOnMouseClicked((event -> {

        }));
        champ3.setOnMouseClicked((event -> {

        }));
        champ4.setOnMouseClicked((event -> {

        }));
        champ5.setOnMouseClicked((event -> {

        }));
        champ6.setOnMouseClicked((event -> {

        }));
        champ7.setOnMouseClicked((event -> {

        }));
        champ8.setOnMouseClicked((event -> {

        }));
        champ9.setOnMouseClicked((event -> {

        }));
        champ10.setOnMouseClicked((event -> {

        }));


        return null;
    }
}

}

And this is the second :

public class ScreenController {

private Participant participant;

@FXML
public ImageView profileIcon;
@FXML
public Label summonerName;
@FXML
public Label level;
@FXML
public ImageView champIcon;
@FXML
public Label masteryPoints;
@FXML
public Label masteryLevel;
@FXML
public Button goBack;

public void initialize() throws IOException, URISyntaxException {
    participant = MainController.participant;


    System.out.println(participant.getSummonerName());
    Summoner summoner = SummonerUtils.getSummoner(participant.getSummonerName(), Region.TR);
    Champion champion = ChampionUtils.getChampion((int) participant.getChampionId());
    Mastery mastery = MasteryUtils.getMastery(summoner.getSummId(), champion.getId(), Region.TR);

    Image profileImage = new Image(new File(summoner.getProfileIconId()).toURI().toString());
    Image champImage = new Image(new File(champion.getIcon()).toURI().toString());
    profileIcon.setImage(profileImage);
    summonerName.setText(summoner.getSummName());
    level.setText(String.valueOf(summoner.getLevel()) + " Level");
    champIcon.setImage(champImage);
    masteryPoints.setText(String.valueOf(mastery.getChampPoints()) + " Points");
    masteryLevel.setText(String .valueOf(mastery.getChampLevel()) + " Level");

    goBack.setOnMouseClicked((event -> {
        try {
            LolAppApplication.mainStage.getScene().setRoot(FXMLLoader.load(getClass().getClassLoader().getResource("main.fxml")));
            new Thread(MainController.downloadTask).start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }));
}

}

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62

1 Answers1

0

You can store these values in an Bundle like object you could name it MyBundle or something and you can make it public singleton so you can check if it has values every time you go to MainController if values are not null load them of not get them and store them in MyBundle
I have done this in a project before and it's a good practice

Ahmed Emad
  • 674
  • 6
  • 19