tags:

views:

139

answers:

4

Hello Guys,

I am having some problems concerning starting javax.swing.Timer after a mouse click. I want to start the timer to perform some animation after the user clicks on a button but it is not working.

Here are the code snippets:

public class ShowMe extends JPanel{
  private javax.swing.Timer timer;

  public ShowMe(){
    timer = new javax.swing.Timer(20, new MoveListener());
  }    

  // getters and setters here

  private class MoveListener implements ActionListener {

    public void actionPerformed(ActionEvent e) {
     // some code here to perform the animation
    }
  }
}

This is the class which contains a button so that when the user clicks on the button the timer starts to begin the animation

public class Test{

 // button declarations go here and registering listeners also here

 public void actionPerformed(ActionEvent e) {
  if(e.getSource() == this.btnConnect){
      ShowMe vis = new ShowMe();
      vis.getTimer().start();
  }
 }
}

I want to start the timer to begin the animation but it is not working.

Need help how to make a timer start after button click.

Thanks.

A: 

You must call the start() method of the timer to start it.

  public ShowMe(){
    timer = new javax.swing.Timer(20, new MoveListener());
    timer.start();
  }  

EDIT:
I have not seen that start() is being called in the Test class...
Next step would be to add some logging/printing to the MouseListener class to check if it is being called or not

  private class MoveListener implements ActionListener {

    public void actionPerformed(ActionEvent e) {
      System.out.println("MouseListener activated");  // TODO delete this line
     // some code here to perform the animation
    }
  }

If it's running (I can't find any reason why not in the posted code), the problem is as Ash wrote above: You created a new instance assigned to vis and started its Timer, but you have not added that instance to any visible container.
(maybe you added another instance of ShowMe earlier in the code...)

Carlos Heuberger
According to the sample code, `start()` is getting called in the action listener that creates the `ShowMe` panel.
Ash
@Ash - thanks, haven't seen it there... I think it shouldn't be there anyway... but changed my answer.
Carlos Heuberger
A: 

Some things to try:

  1. Check that your panel is visible, e.g. make the background color red.

  2. Check that the animation is being updated. For example, if you are animating by drawing different frames in a paint() method, then you will need to call repaint() in your timer, after updating the variables controlling animation. Alternatively, if animation is done by changing layout properties (e.g. to move a component around) then a call to validate() will be needed.

Using swing timer can get you started, but it's really the bare underpinnings. There are also libraries avaialbe that will allow you to go further with less effort:

  1. animated transitions
  2. Trident animation library
mdma
A: 

I know this question is a bit old, but I don't think you got an answer.

I believe the problem is that the ShowMe class and its Timer is being garbage collected, and hence fails to do what you think it should.

You are creating a new local ShowMe variable that goes out of scope as soon as the actionPerformed method completes. The Timer and its ActionListener are local to the ShowMe class instance, so when the actionPerformed method completes, they are also available for GC.

I'm not sure what the ShowMe class is doing. It appears to be a JPanel, so I assume it is something you want to display. It sounds like in your Test class (or real class), it might be better to have a ShowMe data member that you can just call start one when the button is clicked, instead of creating a new one every time.

wolfcastle
A: 

Your usage of the Timer class seems to be correct. Maybe the problem lies in the MoveListener.

Did you remember to use the paintImmediately() method to repaint your animation?

If you use just repaint() you won't see a smooth animation, since repeated calls to repaint() are reduced to one call.

Kintaro