Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to wait until a task has finished and then continue, within the event dispatch thread?
    primarykey
    data
    text
    <p>I have a program, a game, with GUI made with Swing.</p> <p>The GUI is comprised of my own class which inherits JFrame and holds a JPanel with CardLayout, which has multiple JPanels as cards.</p> <p>On one of these panels there is a Button which starts the game and the game view is located on one of the cards (the view object, which has its own class which inherits JPanel, is added to the card in the constructor of the JFrame).</p> <p>Now the prologue:</p> <p>I press the JButton, so I jump to the Event Dispatch Thread to execute the actionPerformed method.</p> <p>In this method I first flip to the card that holds the view panel, then instantiate the level i'm going to play and the game engine (which is instantiated according to the level). Then i call repaint on the view object (which should end up with the game being painted in the apllication window) and THEN I call my game engine object to start the game loop.</p> <p>My problem is:</p> <p>When i press the button, the game engine begins to run properly (i see it from the prints in console), but my GUI freezes and won't refresh to show the game view. When i remove the snippet of code that starts the game loop, the game view shows on the screen normally, nothing moves though, because the game loop hasn't been started. </p> <p>So I have deducted that, because in Java everything doesn't happen in the order it is written, the game loop begins to run BEFORE the other operations in the actionperformed method have been completed.</p> <p>Here is the relevant snippet from the actionperformed method kortti = card kentta = level valikkopaneeli = the panel that has cardlayout aloitusnappula = the button that starts the game Peli = the class i made that inherits JFrame</p> <pre><code>if (e.getSource() == aloitusnappula) { </code></pre> <p>CardLayout kortti = (CardLayout)valikkopaneeli.getLayout(); Kentta kentta = new Kentta("testikentta.txt"); kortti.show(valikkopaneeli, "Peli"); Peli.this.pack(); Peli.this.setVisible(true); aloita(kentta); }</p> <p>and here is a snippet of the aloita(kentta) method:</p> <pre><code>public void aloita(Kentta kentta) { moottori = new Pelimoottori(Peli.this, kentta); nakyma.repaint(); moottori.peliLooppi(); } </code></pre> <p>where Pelimoottori = the Game Engine class nakyma = the view object peliLooppi() = the method in the Game Engine class that starts the Game Loop</p> <p>If I'd remove moottori.peliLooppi() call from the aloita method, the game view would show normally.</p> <p>I tried to google some answers and the farthest I've got is somehow doing it with SwingWorker, but somehow it doesn't sound like a good way of doing what i want.</p> <p>Any suggestions?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload