Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are a number of important concepts you seem to be missing.</p> <ol> <li>Swing is an event driven environment. That means that there is no means (or at least only a very few) that you can "wait" for user input, typically, you just need to react to their interaction.</li> <li>Swing is driven by a single thread, known as the Event Dispatching Thread (AKA EDT). It is the responsibility of this thread to dispatch/process events coming into the application to the appropriate parts of the application so that they can take action.</li> <li>The repaint manager posts its update requests onto the EDT.</li> </ol> <p>ANY action you take that stops the EDT from carrying out this work will make your application look like it's hung.</p> <p>You must NEVER carry out any time consuming operations (such as I/O, loops or <code>Thread#sleep</code> for example) on the EDT, doing so will make your application "pause", which is never pretty.</p> <p>Have a read through <a href="http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html" rel="nofollow noreferrer">Concurrency in Swing</a> for more information.</p> <p>Now, you have a number of choices. You could use a <code>Thread</code> to "wait" in the background and turn the cards back or you could use a <code>SwingWorker</code> or a <code>javax.swing.Timer</code>.</p> <p>The other problem you have, is that you should NEVER update any UI components from any <code>Thread</code> other than the EDT. This means if you were to use a <code>Thread</code>, you would become responsible for re-syncing that thread with the EDT. While not difficult, it just becomes messy.</p> <p><code>SwingWorker</code> and <code>javax.swing.Timer</code> have functionality that make this much easier.</p> <p>Threads and <code>SwingWorker</code> are great for performing background processing and would simply be overkill for this problem. Instead, a <code>javax.swing.Timer</code> would fit perfectly here.</p> <pre><code>if (!Control.model.twoCardsTurned) { if ("unturn".equals(action)) { new Timer(1000, new ActionListener() { public void actionPerformed(ActionEvent evt) { keypadArray[pos].setIcon(back); keypadArray[Control.model.lastCard].setIcon(back); System.out.println("Card: "+pos+" unset"); System.out.println("Card: "+Control.model.lastCard+" unset"); } }).start(); } } </code></pre> <p>This is a really simple example. You might like to put in some controls that will prevent the user from clicking anything until the timer fires, for example ;)</p>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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