This discussion is archived
4 Replies Latest reply: Nov 23, 2012 11:47 AM by jsmith RSS

Simple KeyFrame Question

971758 Newbie
Currently Being Moderated
Okay, I am looking at the API, and maybe I don't understand it as well as I think I do with regards to the KeyFrame constructor.


http://docs.oracle.com/javafx/2/api/javafx/animation/KeyFrame.html

It says it looks something like this

KeyFrame(Duration time, EventHandler<ActionEvent> onFinished, KeyValue... values)



so I have

final KeyFrame kf = new KeyFrame(Duration.millis(2000),onFinished(new EventHandler<ActionEvent>() {

                       @Override
                       public void handle(ActionEvent t) {
                               throw new UnsupportedOperationException("Not supported yet.");
                       }
               } ),kv);
of course netbeans says cannot find method onFinished.

so I tried

final KeyFrame kf = new KeyFrame(Duration.millis(2000),EventHandler<ActionEvent> onFinished ,kv);
Netbeans does not like this either.

saying it cannot find symbol:EventHandler.

I have my imports set up though
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
So basically, there is something I do not understand very well and I would like it explained to me in a clear and thorough way that makes sense. And I would like a solution to my problem of adding passing an event handler just like the constructor says I can. Please remember my education and/or experience with this may not be as extensive as yours. To be honest, no one really explained how these Handlers work and I would like to understand.
  • 1. Re: Simple KeyFrame Question
    jsmith Guru
    Currently Being Moderated
    Try using an anonymous class to define your handler.
    http://stackoverflow.com/questions/355167/how-are-anonymous-inner-classes-used-in-java
    http://docstore.mik.ua/orelly/java-ent/jnut/ch03_12.htm
    final KeyFrame kf = new KeyFrame(
      Duration.millis(2000), 
      new EventHandler<ActionEvent>() {
        @Override public void handle(ActionEvent t) {
          System.out.println("Handled: " + t);
        }
       },
      kv
    );
    To understand what is going on here you may want to read this:
    http://www.joelonsoftware.com/items/2006/08/01.html
    The anonymous inner class with a single defined argument is a mechanism to pass an inlined function as an argument in Java; a language in which objects, not functions, are first class citizens. Java8 will provide, via the lambda project, an alternate syntax for doing this.
  • 2. Re: Simple KeyFrame Question
    971758 Newbie
    Currently Being Moderated
    Okay, thank you. That works. Is there a way to do it without the inner method thing going on?
  • 3. Re: Simple KeyFrame Question
    jsmith Guru
    Currently Being Moderated
    final KeyFrame kf = new KeyFrame(
      Duration.millis(2000), 
      new EventPrinter(),
      kv
    );
    ...
    class EventPrinter implements EventHandler<ActionEvent> {
      @Override public void handle(ActionEvent t) {
        System.out.println("Handled: " + t);
      }
    }
  • 4. Re: Simple KeyFrame Question
    971758 Newbie
    Currently Being Moderated
    Thanks so much.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points