casper04
casper04

Reputation: 73

Java: change JPanel while saving game

I'm making a small game and I've already implemented a save function in which the game is saved (by writing information to a new XML file). The saving takes a couple of seconds and I want to do the following: while the program is saving the game, I want to change the look of the JPanel, and when it is done saving, I want to go back to another page(show another JPanel). I have the following code:

confirm.addActionListener(new ActionListener () {
        @Override
        public void actionPerformed(ActionEvent e){
            String fileNaam = saveGame.getText();
            //This method changes the look of the panel 
            changePanel();
            //This method saves the game
            model.saveGame(fileNaam);
            //This method takes the user back to a previous page
            controller.viewTeamPage();
        }
});

What happens is that the game is saved and the user is taken back to the teampage, but the panel is never changed. The changePanel() method does work, so that is not the problem but it seems like it is never executed. I was wondering if somebody knows how I can fix this.

EDIT:

private void changePanel () {
    panel.removeAll();
    panel.repaint();
    panel.revalidate();
}

This is the method to change the look of the panel, for now I just remove everything on the panel to keep it simple. Also, the saving is not done in a separate Thread, is that something I should look at?

EDIT 2: I fixed it by using a thread to save the game and return to the teampage after the saving is done. See the code below.

confirm.addActionListener(new ActionListener () {
        @Override
        public void actionPerformed(ActionEvent e){
            final String fileNaam = saveGame.getText();
            changePanel();
            Thread t = new Thread (new Runnable () {
                @Override
                public void run() {
                    model.saveGame(fileNaam);
                    controller.viewTeamPage();
                }
            });
            t.start();
        }
});

Upvotes: 0

Views: 140

Answers (2)

ulix
ulix

Reputation: 999

  1. Call your save game method from a new thread but don't "join" or "try" to wait for this thread to finish from inside the method actionPerformed();
  2. Make the call to controller.viewTeamPage() after the save game thread is done saving the game. One simple way of doing that would be passing the "controller" object to the constructor of your custom thread so you can make that call after saving the game.

The step 1 is very important in this case because all the calls you are making in the method actionPerformed() are being made in the UI thread, preventing the entire UI from refreshing until the method returns. Even calling repaint() alone, in changePanel(), wont be enough because it just "schedules" a refresh on you panel that will only happen after actionPerformed() returns. If you put the most time consuming call in a separate thread however, the actionPerformed() returns quickly allowing the UI to be refreshed while the game saving thread is doing its job.

Upvotes: 0

SectorCodec
SectorCodec

Reputation: 186

If you are changing the same panel and not intializing a new panel then the problem i think is that you need to call the panel.revalidate or panel.repaint i think. I made a demo for a Procedural generation project and i had to do this to make my panel change.

Upvotes: 1

Related Questions