Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Using swing timers from actionListeners

Reply
Thread Tools

Using swing timers from actionListeners

 
 
stinkinrich88@googlemail.com
Guest
Posts: n/a
 
      02-28-2007
Hello. I have the following class:

import javax.swing.*;
import java.awt.event.*;

class Die extends JLabel implements ActionListener
{
private int counter;
private Timer dieTm = new Timer(70, this);
private int dieValue;
private ImageIcon[] dieImg = new ImageIcon[6];

public Die()
{
ClassLoader cldr = this.getClass().getClassLoader();
for(int i = 1; i<7; i++)
dieImg[i-1] = new ImageIcon(cldr.getResource(i +".gif"));
}

public int doRoll()
{
setVisible(true);
counter = 0;
dieTm.start();

while(dieTm.isRunning());

dieValue = (int)Math.ceil((Math.random()*6));

setIcon(dieImg[dieValue-1]);
return dieValue;
}

public void actionPerformed(ActionEvent e)
{
setIcon(dieImg[(int)Math.ceil((Math.random()*6))-1]);
counter++;
System.out.println(counter);
if (counter == 10) dieTm.stop();
}

}

That should animate a dice rolling and stop at a value. It works fine
when I call it within the code, but when I call it from an action
Listener the timer's action listener doesn't run, but the timer
isRunning returns true so it ends up in an infinite While loop waiting
for it to finish.

Any ideas? Thanks!

 
Reply With Quote
 
 
 
 
Daniel Pitts
Guest
Posts: n/a
 
      03-01-2007
On Feb 28, 8:01 am, "stinkinric...@googlemail.com"
<stinkinric...@googlemail.com> wrote:
> Hello. I have the following class:
>
> import javax.swing.*;
> import java.awt.event.*;
>
> class Die extends JLabel implements ActionListener
> {
> private int counter;
> private Timer dieTm = new Timer(70, this);
> private int dieValue;
> private ImageIcon[] dieImg = new ImageIcon[6];
>
> public Die()
> {
> ClassLoader cldr = this.getClass().getClassLoader();
> for(int i = 1; i<7; i++)
> dieImg[i-1] = new ImageIcon(cldr.getResource(i +".gif"));
> }
>
> public int doRoll()
> {
> setVisible(true);
> counter = 0;
> dieTm.start();
>
> while(dieTm.isRunning());
>
> dieValue = (int)Math.ceil((Math.random()*6));
>
> setIcon(dieImg[dieValue-1]);
> return dieValue;
> }
>
> public void actionPerformed(ActionEvent e)
> {
> setIcon(dieImg[(int)Math.ceil((Math.random()*6))-1]);
> counter++;
> System.out.println(counter);
> if (counter == 10) dieTm.stop();
> }
>
> }
>
> That should animate a dice rolling and stop at a value. It works fine
> when I call it within the code, but when I call it from an action
> Listener the timer's action listener doesn't run, but the timer
> isRunning returns true so it ends up in an infinite While loop waiting
> for it to finish.
>
> Any ideas? Thanks!


do you ever call dieTime.start()?

Can you give use an <sscce> that we can use to recreate the problem?
<http://physci.org/codes/sscce>

 
Reply With Quote
 
 
 
 
Daniel Pitts
Guest
Posts: n/a
 
      03-01-2007
On Feb 28, 8:01 am, "stinkinric...@googlemail.com"
<stinkinric...@googlemail.com> wrote:
> Hello. I have the following class:
>
> import javax.swing.*;
> import java.awt.event.*;
>
> class Die extends JLabel implements ActionListener
> {
> private int counter;
> private Timer dieTm = new Timer(70, this);
> private int dieValue;
> private ImageIcon[] dieImg = new ImageIcon[6];
>
> public Die()
> {
> ClassLoader cldr = this.getClass().getClassLoader();
> for(int i = 1; i<7; i++)
> dieImg[i-1] = new ImageIcon(cldr.getResource(i +".gif"));
> }
>
> public int doRoll()
> {
> setVisible(true);
> counter = 0;
> dieTm.start();
>
> while(dieTm.isRunning());
>
> dieValue = (int)Math.ceil((Math.random()*6));
>
> setIcon(dieImg[dieValue-1]);
> return dieValue;
> }
>
> public void actionPerformed(ActionEvent e)
> {
> setIcon(dieImg[(int)Math.ceil((Math.random()*6))-1]);
> counter++;
> System.out.println(counter);
> if (counter == 10) dieTm.stop();
> }
>
> }
>
> That should animate a dice rolling and stop at a value. It works fine
> when I call it within the code, but when I call it from an action
> Listener the timer's action listener doesn't run, but the timer
> isRunning returns true so it ends up in an infinite While loop waiting
> for it to finish.
>
> Any ideas? Thanks!


Ah, I see the problem...

doRoll blocks until dieTm.stop() is called... The problem is, that
the Timer event won't be able to be dispatched until doRoll returns,
which means that actionPerformed won't be called, which means
dieTm.stop() won't be called, which means... DEADLOCK!

public int doRoll() {
setVisible(true);
counter = 0;
dieValue = (int)Math.ceil((Math.random()*6));
dieTm.start();
return dieValue;
}

public void actionPerformed(ActionEvent e) {
setIcon(dieImg[(int)Math.ceil((Math.random()*6))-1]);
counter++;
System.out.println(counter);
if (counter == 10) {
dieTm.stop();
setIcon(dieImg[dieValue-1]);
return dieValue;
}
}

 
Reply With Quote
 
stinkinrich88@googlemail.com
Guest
Posts: n/a
 
      03-01-2007
On Feb 28, 8:33 pm, "Daniel Pitts" <googlegrou...@coloraura.com>
wrote:
> On Feb 28, 8:01 am, "stinkinric...@googlemail.com"
>
>
>
> <stinkinric...@googlemail.com> wrote:
> > Hello. I have the following class:

>
> > import javax.swing.*;
> > import java.awt.event.*;

>
> > class Die extends JLabel implements ActionListener
> > {
> > private int counter;
> > private Timer dieTm = new Timer(70, this);
> > private int dieValue;
> > private ImageIcon[] dieImg = new ImageIcon[6];

>
> > public Die()
> > {
> > ClassLoader cldr = this.getClass().getClassLoader();
> > for(int i = 1; i<7; i++)
> > dieImg[i-1] = new ImageIcon(cldr.getResource(i +".gif"));
> > }

>
> > public int doRoll()
> > {
> > setVisible(true);
> > counter = 0;
> > dieTm.start();

>
> > while(dieTm.isRunning());

>
> > dieValue = (int)Math.ceil((Math.random()*6));

>
> > setIcon(dieImg[dieValue-1]);
> > return dieValue;
> > }

>
> > public void actionPerformed(ActionEvent e)
> > {
> > setIcon(dieImg[(int)Math.ceil((Math.random()*6))-1]);
> > counter++;
> > System.out.println(counter);
> > if (counter == 10) dieTm.stop();
> > }

>
> > }

>
> > That should animate a dice rolling and stop at a value. It works fine
> > when I call it within the code, but when I call it from an action
> > Listener the timer's action listener doesn't run, but the timer
> > isRunning returns true so it ends up in an infinite While loop waiting
> > for it to finish.

>
> > Any ideas? Thanks!

>
> Ah, I see the problem...
>
> doRoll blocks until dieTm.stop() is called... The problem is, that
> the Timer event won't be able to be dispatched until doRoll returns,
> which means that actionPerformed won't be called, which means
> dieTm.stop() won't be called, which means... DEADLOCK!
>
> public int doRoll() {
> setVisible(true);
> counter = 0;
> dieValue = (int)Math.ceil((Math.random()*6));
> dieTm.start();
> return dieValue;
>
> }
>
> public void actionPerformed(ActionEvent e) {
> setIcon(dieImg[(int)Math.ceil((Math.random()*6))-1]);
> counter++;
> System.out.println(counter);
> if (counter == 10) {
> dieTm.stop();
> setIcon(dieImg[dieValue-1]);
> return dieValue;
> }
>
> }


Ahhh! Genius! Thank you very much for both of your help! It works
well!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why not using javax.swing.event with swing? S.T Java 2 05-25-2007 12:10 AM
Counting the number of hits of a field-any actionlisteners available?? Sumon.Mukherjee@gmail.com Java 0 03-06-2007 01:09 PM
Swing Model Classes Updating Swing Components on a Thread Other Than AWT mkrause Java 0 05-06-2005 04:32 PM
Java 1.2 Swing vs. Java 1.5 Swing Big Daddy Java 2 04-16-2005 01:14 PM
How to Discover & remove ActionListeners Yin99 Java 2 01-27-2005 02:07 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57