This website uses cookies for visitor traffic analysis. By using the website, you agree with storing the cookies on your computer.More information

Better Lambda support for JVx

I wrote about JVx and Java 8, Events and Lambdas some weeks ago.

It was a first test with JVx events and Lambdas and it was great to see that everything worked as expected, BUT not all JVx events were supported because not all listeners were defined as SAM type.

It was great to see that Lambdas were supported with our event handling implementation but it was a shame that only action events and some model events were really supported. So we started thinking :)

Our key listener was defined like this:

public interface IKeyListener
{
    public void keyTyped(UIKeyEvent pKeyEvent);

    public void keyPressed(UIKeyEvent pKeyEvent);

    public void keyReleased(UIKeyEvent pKeyEvent);
}

The problem was that there were 2 methods too much. So we split the interface in 3 new interfaces:

  • IKeyPressedListener
  • IKeyReleasedListener
  • IKeyTypedListener

Every listener has exactly one method, e.g.

public interface IKeyPressedListener
{
    public void keyPressed(UIKeyEvent pKeyEvent);
}

The new listener, for backwards compatibility, is this:

public interface IKeyListener extends IKeyTypedListener,
                                      IKeyPressedListener,
                                      IKeyReleasedListener
{
}

Our listener are handled from event handlers, like KeyHandler. The old implementation was:

public class KeyHandler extends RuntimeEventHandler<IKeyListener>
{
    public KeyHandler(String pListenerMethodName)
    {
        super(IKeyListener.class, pListenerMethodName);
    }
}

(The handler creation with a method name wasn't really typesafe but it worked)

We refactored the handler a little bit and the result is:

public class KeyHandler<L> extends RuntimeEventHandler<L>
{
    public KeyHandler(Class<L> pClass)
    {
        super(pClass);
    }  
}

After some refactoring, our listeners were Lambda and backwards compatible, e.g.:

userName.eventKeyPressed().addListener(e -> System.out.println(e.getKeyChar()));

The next nightly build will contain all changes!
There are still some smaller todos, but Lambdas are well supported.