user6738647
user6738647

Reputation:

Using a swing timer in java

I have not been coding in java for long, a month at most, but I have coded in multiple other object oriented languages.

I am trying to use the swing timer included in java.swing.timer, and I read https://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html to try and understand it.

I understand that with timer = new Timer(speed, this), speed is how often the timer fires, but I do not understand what this means. Also, I know that public void actionPerformed(ActionEvent e) will execute every time the timer fires, but is there a way for me to use two timers in the same class?

Any help is appreciated, thank you in advance.

Upvotes: 0

Views: 3348

Answers (2)

GhostCat
GhostCat

Reputation: 140427

The javadoc for Timer tells you what that constructor is about - it takes an int and an instance of ActionListener.

So, to answer your first question, in the example code from Oracle, timer = new Timer(speed, this) happens within the init method of that Applet that makes up the example UI thingy. Thus: this refers to the "current" object (see here for more details), being that Applet object the whole method "belongs" to.

And that only works because that example class TumleItem is declared as TumbleItem extends JApplet implements ActionListener.

For the record: understanding what this is about is absolutely basic stuff. If you don't know what this is about, then you shouldn't be engaging in UI coding yet. Learn to crawl before you try to run.

For your second question: for each instance of a Timer you want to use, you need an ActionListener that you can pass in. The old-school way of doing that would be to use anonymous inner classes, like

timer = new Timer(someSpeed, new ActionListiner() {
   @Override
   public void actionPerformed(ActionEvent evt) {
      System.out.println("Whatever");
   }
}

Finally: that one example from Oracle is using Applets, and simply spoken: Applets are dead technology. Don't get too hung up on them; you better avoid spending time on those completely.

Upvotes: 2

Greg Kopff
Greg Kopff

Reputation: 16545

The constructor you're invoking has this signature:

public Timer(int delay, ActionListener listener)

This means that, in the example, this refers to an instance of the ActionListener interface.

In your example, the class containing the timer code happens to implement ActionListener. This is simply for the convenience of having a succinct example.

You can pass any instance of an ActionListener to your timer. If you're using Java 8, this is very simple using method references.

public class TheExample
{
  public TheExample()
  {
    final Timer timerOne = new Timer(speedOne, this::timerOneMethod);
    final Timer timerTwo = new Timer(speedTwo, this::timerTwoMethod);
  }

  private void timerOneMethod(ActionEvent e)
  {
    // do something exciting
  }

  private void timerTwoMethod(ActionEvent e)
  {
    // do something else exciting
  }
}

If you're not using Java 8, the same thing can be achieved using anonymous inner classes. You can find an example of this way here.

Upvotes: 4

Related Questions