Java Timer problem

jmastic

n00b
Joined
Apr 24, 2006
Messages
11
Hey all! I'm hoping someone can help me figure this out because I'm a bit confused.

I am setting up a timer which bases its timeout value on a variable.

Here is the code in question:

Code:
int processTime = 5000;

javax.swing.Timer goToMask;

goToMask = new javax.swing.Timer(processTime, new ActionListener() {
          public void actionPerformed(ActionEvent e) {
                goToMask.stop();
                // Rest of code to execute after timeout
            }
         });

If I hard-code the value '5000' in for "processTime", it works perfectly, but I need to be able to change the processTime value, so hard-coding the value is not an option.

Right now it doesn't seem like the timer waits at all. I know that the code located in the ActionListener class gets executed when i call goToMask.start(), but it doesn't have the right wait time.

Any help would be greatly appreciated! Thanks!
 
So you're saying "right now it doesn't seem like the timer waits at all" in reference to your current code that doesn't have a hard-coded value? If so, can you share that code so we know what it is?
 
Of course, Tawnos!

Code:
goToMask = new javax.swing.Timer(processTime, new ActionListener() {
          public void actionPerformed(ActionEvent e) {
                  goToMask.stop();
                  panel04.setVisible(false);
                  maskPanel.setVisible(true);
            }
       });

That's the full code for the timer. I know that this code is being run because what I called panel04 is getting hidden and the "maskPanel" is being shown. So, the code inside of the ActionListener is being executed, but it is being done immediately and not waiting the "processTime" value.

What I was doing before, what I called hard-coding, looked like this:

Code:
goToMask = new javax.swing.Timer(5000, new ActionListener() {
          public void actionPerformed(ActionEvent e) {
                  goToMask.stop();
                  panel04.setVisible(false);
                  maskPanel.setVisible(true);
            }
       });

And this worked the way I want, with a delay before the code is executed, but I can't specify the "5000" ms.

Does that clear up my situation a little bit?

Thanks for the reply!
 
I may have answered my own question on this one.

Do you know if you need to make the "processTime" variable final in order for the Timer class to use it? I made another variable:

Code:
final int processTime2 = 5000;

and placed it into the code I posted earlier, and it works that way.

So, if this variable HAS to be a final int, then is there any way I can make this processTime2 reference back to whatever value is in processTime (the first one) so the value is changing based on what I input?

Or am I missing something else entirely?
 
Finding out that last piece of information helped a lot.

Here is my solution:

Code:
goToMask.setInitialDelay(processTime);
goToMask.start();

I placed this in my code so that every time that the timer is started, it sets the initial delay, which happens to be the only delay I want. I also reverted my goToMask timer definition to my "hard-coded" time, and put in an arbitrary number. It won't matter in the end.

This initial delay doesn't appear to need a final variable to work, so this worked perfectly for me.

Thanks for your reply, Tawnos!
 
Don't think I really did anything, but if it helped, you're welcome?
 
Back
Top