Note that there are some explanatory texts on larger screens.

plurals
  1. POCounter to Close JFrame, bring up Confirm Dialog
    primarykey
    data
    text
    <p>This post relates to my last one regarding a timer. I decided the easiest thing to do for immediate results was to just write a Counter thread that counts down from a certain time (in this case 5 seconds) and if the counter reaches 0, the JFrame closes and let's the user know that time has expired. </p> <p>I'm running into some trouble, however. I cannot seem to make the JFrame close when the counter reaches 0. I'm not sure if I'm missing something stupid or if I am misunderstanding the way threads work and the way JFrames work. Here is the code, let me know what you think.</p> <p>On a side note, I understand it would probably be most efficient to use a swing.Timer, but I just don't quite grasp the nature of them yet. I'm under self-imposed time constraints (I'm not a student or anything, I just like to stay motivated) and so I'm "jerry-rigging" this thing for now.</p> <p>Anyway, on to the code!</p> <pre><code>import javax.swing.*; import javax.swing.event.*; import java.awt.*; public class RacerDoom extends JFrame { boolean timesUp=false; public RacerDoom() { //create JFrame super("Racer Doom Squared"); setSize(WIDTH,HEIGHT); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); if(timesUp==true) { dispose(); JOptionPane.showConfirmDialog(null, "Time's Up! Click Okay to try again!"); } Counter c1 = new Counter(); c1.start(); //Counter private class Counter extends Thread { public Counter() {} public void run() { for(int i=5;i&gt;=0;i--) { if(i==0) { timesUp=true; } System.out.println(i); try{ Thread.sleep(1000); } catch(InterruptedException e){} } } } ... </code></pre> <p>EDIT: I have the timer implemented and working. It does exactly what I need it to, but I can't get the <code>timer.stop();</code> command to work. I get the error "The local variable timer may not have been initialized.</p> <p>Like I said, the timer works, it just never stops working until the program is terminated. Here is the constructor code for the JFrame, where the timer is located.</p> <pre><code>int counter = 0; public RacerDoom() { //create JFrame super("Racer Doom Squared"); setSize(WIDTH,HEIGHT); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); final Timer timer=new Timer(1000, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if(counter&gt;=10) { timer.stop(); //the error occurs here dispose(); JOptionPane.showConfirmDialog(null, "Time's Up!"); } else{ counter++; } System.out.println(counter); } }); //inner thread Move1 m1 = new Move1(); m1.start(); timer.start(); } </code></pre>
    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.
 

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